本文主要是介绍Java面向接口编程——开发打印机,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
墨盒:彩色、黑白
纸张类型:A4、B5
墨盒和纸张都不是打印机厂商提供的
打印机厂商要兼容市场上的墨盒、纸张
墨盒接口:
public interface InkBox {String colorInkBox();
// 墨盒颜色}
纸张接口:
public interface Paper {String size();
// 纸张大小
}
A4纸类:
package QuestionPpt03;public class A4paper implements Paper{public String size(){return "A4纸张";}
}
B5纸类:
public class A5Paper implements Paper{public String size(){return "B5纸张";}
}
彩色墨盒类:
public class CorlorfulInk implements InkBox{public String colorInkBox(){return "彩色墨盒";}
}
黑白墨盒类:
public class BlackInk implements InkBox{public String colorInkBox(){return "黑白墨盒";}
}
打印机类:
public class Printer {public void print(Paper paper ,InkBox inkBox){System.out.println("在纸张大小为:"+paper.size()+"的纸上打印颜色为:"+inkBox.colorInkBox());}
}
测试类:
这里用了接口的多态,可以注意一下
public class Test {public static void main(String[] args) {// 打印机实例Printer pinter = new Printer();
// A5纸:Paper a5Paper = new A5Paper();
// A4纸:Paper a4paper = new A4paper();
// 黑白墨盒:CorlorfulInk corlorfulInk = new CorlorfulInk();InkBox blackInk = new BlackInk();
// 彩色墨盒://pinter.print(a5Paper,blackInk);pinter.print(a4paper,corlorfulInk);pinter.print(a4paper,blackInk);}
}
这篇关于Java面向接口编程——开发打印机的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!