day7_work1_printer
作业
面向接口编程思想的打印机:打印机,(黑.彩色墨盒),(a4.b5纸张)
- 定义接口:墨盒, 纸张
- 按接口开发类
- 将五个类按需要组装并使用
类图:
代码:
墨盒接口:
public interface Ink{
public String getColor();
}
黑色墨盒类:
public class BlackInk implements Ink{
@Override
public String getColor() {
return "黑色墨盒";
}
}
彩色墨盒类:
public class ColorInk implements Ink{
@Override
public String getColor() {
return "彩色墨盒";
}
}
纸张接口:
public interface Paper{
public String getSize();
}
A4纸类:
public class A4Paper implements Paper{
@Override
public String getSize() {
return "A4纸";
}
}
B5纸类:
public class B5Paper implements Paper{
@Override
public String getSize() {
return "B5纸";
}
}
打印机类:
public class Printer {
private Paper paper;
private Ink ink;
private String brand;
public Printer(String brand) {
this.brand = brand;
}
public print(String words) {
System.out.println(brand + "牌打印机正在使用" + ink.getColor() + "和" + paper.getSize() + "打印:");
System.out.println(words);
}
public void setPaper(Paper paper) {
this.paper = paper;
}
public Paper getPaper() {
return paper;
}
public void setInk(Ink ink) {
this.ink = ink;
}
public Ink getInk() {
return ink;
}
public String getBrand() {
return brand;
}
}
客户类:
public class Customer {
public static void main(String[] args) {
//采购
Paper a4Paper = new A4Paper();
Paper b5Paper = new B5Paper();
Ink blackInk = new BlackInk();
Ink colorInk = new ColorInk();
Printer printer = new Printer("海尔");
//组装
printer.setPaper(a4paper);
printer.setInk(blackInk);
//使用
printer.print("呜呼");
}
}