接口:Printable 方法:print() 抽象类:1:BlackPrinter 实现 Printable ,print输出:黑白打印;2:ColorPrinter 实现 Printable ,print输出:彩色打印 具体类:1:LaserPrinter,继承 BlackPrinter,输出:黑白打印,激光打印;具体类 2:JetInkPrinter 继承 ColorPrinter ,print输出:彩色打印,喷墨打印 测试类:声明接口Printable 变量p,new一个激光打印机,一个喷墨打印机,分别用p指向两个打印机,用p调用print方法
时间: 2023-06-17 15:04:30 浏览: 103
代码如下:
```java
// Printable接口
public interface Printable {
void print();
}
// BlackPrinter实现Printable接口
public class BlackPrinter implements Printable {
@Override
public void print() {
System.out.println("黑白打印");
}
}
// ColorPrinter实现Printable接口
public class ColorPrinter implements Printable {
@Override
public void print() {
System.out.println("彩色打印");
}
}
// LaserPrinter继承BlackPrinter具体类
public class LaserPrinter extends BlackPrinter {
@Override
public void print() {
super.print();
System.out.println("激光打印");
}
}
// JetInkPrinter继承ColorPrinter具体类
public class JetInkPrinter extends ColorPrinter {
@Override
public void print() {
super.print();
System.out.println("喷墨打印");
}
}
// 测试类
public class PrinterTest {
public static void main(String[] args) {
Printable p;
p = new LaserPrinter();
p.print();
p = new JetInkPrinter();
p.print();
}
}
```
输出结果为:
```
黑白打印
激光打印
彩色打印
喷墨打印
```
阅读全文