打印机
打印机类
1 package cn.print; 2 //打印机类 3 public class Printer { 4 private Ink ink; 5 private Paper paper; 6 public void print() { 7 System.out.println("正在使用"+ink.getClours()+"的墨盒在"+paper.getSize()+"纸上打印信息"); 8 } 9 10 public Ink getInk() { 11 return ink; 12 } 13 public void setInk(Ink ink) { 14 this.ink = ink; 15 } 16 public Paper getPaper() { 17 return paper; 18 } 19 public void setPaper(Paper paper) { 20 this.paper = paper; 21 } 22 }
墨盒接口
1 package cn.print; 2 3 public interface Ink { 4 public String getClours(); 5 }
彩色类
1 package cn.print; 2 3 public class ClourInk implements Ink { 4 public String getClours() { 5 return "彩色"; 6 } 7 }
黑白类
1 package cn.print; 2 3 public class GrayInk implements Ink { 4 public String getClours() { 5 return "灰色"; 6 } 7 }
纸接口
1 package cn.print; 2 3 public interface Paper { 4 public String getSize(); 5 }
A4类
1 package cn.print; 2 3 public class A4Paper implements Paper{ 4 public String getSize() { 5 return "A4"; 6 } 7 }
B5类
1 package cn.print; 2 3 public class B5Paper implements Paper { 4 public String getSize() { 5 return "B5"; 6 } 7 }
测试类
1 package cn.print; 2 3 public class Test { 4 public static void main(String[] args) { 5 Printer printer = new Printer(); 6 Paper paper = new A4Paper(); 7 Ink ink = new ClourInk(); 8 printer.setInk(ink); 9 printer.setPaper(paper); 10 printer.print(); 11 } 12 }