public interface InkBox {
// 获取墨盒的颜色
public String getColor();
}
public interface Paper {
//获取纸张大小
public String getSize();
}
public class ImIntBoxcs implements InkBox {
@Override
public String getColor() {
//墨盒实现类 彩色墨盒
return "彩色";
}
}
public class ImIntBoxhb implements InkBox {
@Override
public String getColor() {
//墨盒实现类 黑白墨盒
return "黑白";
}
}
public class ImPaperA4 implements Paper {
@Override
public String getSize() {
//纸张实现类 A4纸张
return "A4";
}
}
public class ImPaperB5 implements Paper {
@Override
public 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);
}
}