题目:
墨盒:彩色、黑白
纸张类型:A4、B5
墨盒和纸张都不是打印机厂商提供的
打印机厂商要兼容市场上的墨盒、纸张
墨盒接口:
java
public interface InkBox {
String colorInkBox();
// 墨盒颜色
}
纸张接口:
java
public interface Paper {
String size();
// 纸张大小
}
A4纸类:
java
package QuestionPpt03;
public class A4paper implements Paper{
public String size(){
return "A4纸张";
}
}
B5纸类:
java
public class A5Paper implements Paper{
public String size(){
return "B5纸张";
}
}
彩色墨盒类:
java
public class CorlorfulInk implements InkBox{
public String colorInkBox(){
return "彩色墨盒";
}
}
黑白墨盒类:
java
public class BlackInk implements InkBox{
public String colorInkBox(){
return "黑白墨盒";
}
}
打印机类:
java
public class Printer {
public void print(Paper paper ,InkBox inkBox){
System.out.println("在纸张大小为:"+paper.size()+"的纸上打印颜色为:"+inkBox.colorInkBox());
}
}
测试类:
这里用了接口的多态,可以注意一下
java
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);
}
}