public interface InkBox {// 获取墨盒的颜色public String getColor();}
public interface Paper {//获取纸张大小public String getSize();}
public class ImIntBoxcs implements InkBox {@Overridepublic String getColor() {//墨盒实现类 彩色墨盒return "彩色";}}
public class ImIntBoxhb implements InkBox {@Overridepublic String getColor() {//墨盒实现类 黑白墨盒return "黑白";}}
public class ImPaperA4 implements Paper {@Overridepublic String getSize() {//纸张实现类 A4纸张return "A4";}}
public class ImPaperB5 implements Paper {@Overridepublic String getSize() {//纸张实现类 B5纸张return "B5";}}
public class Printer {//打印机类 打印参数使用的是接口public void print(InkBox inkbox,Paper paper){System.out.println("使用"+inkbox.getColor()+"的墨盒在"+paper.getSize()+"纸张上打印");}}
public class TestPrinter {public static void main(String[] args) {//声明纸张和墨盒对象InkBox inkbox = null;Paper paper=null;//创建打印机类对象Printer p = new Printer();//彩色墨盒A4纸进行打印inkbox = new ImIntBoxcs();paper = new ImPaperA4();//调用打印方法p.print(inkbox, paper);System.out.println("====================");//黑白墨盒并b5纸进行打印inkbox = new ImIntBoxhb();paper = new ImPaperB5();//调用打印方法p.print(inkbox, paper);}}
结果使用彩色的墨盒在A4纸张上打印====================使用黑白的墨盒在B5纸张上打印