package Print; public interface Ink { void ink(String content); }
package Print; public class BlackInk implements Ink{ @Override public void ink(String content) { System.out.println("使用黑墨水"); } }
package Print; public class ColorfulInk implements Ink{ @Override public void ink(String content) { System.out.println("使用彩墨水"); } }
package Print; public interface Paper{ void paper(String content); }
package Print; public class A4Paper implements Paper{ @Override public void paper(String content) { System.out.println("使用A4纸打印"); } }
package Print; public class B5Paper implements Paper{ @Override public void paper(String content) { System.out.println("使用B5纸打印"); } }
package Print; public class Print { private Ink ink; private Paper paper; public Paper getPaper() { return paper; } public void setPaper(Paper paper) { this.paper = paper; } public Ink getInk() { return ink; } public void setInk(Ink ink) { this.ink = ink; } public void print(String content){ System.out.println("开始打印:"); ink.ink(content); paper.paper(content); System.out.println("打印结果:"+content); System.out.println("打印结束"); } }
package Print; public class PrintTest { public static void main(String[] args) { Print print = new Print(); print.setInk(new BlackInk()); print.setPaper(new A4Paper()); print.print("567"); } }