类主要结构图
- 抽象类:Printer(打印机作为父类)
- 子类:DotMatrixPrinter(针式打印机类)、InkpetPrinter(喷墨打印机类)、LaserPrinter(激光打印机类)
- 测试类:PrinterTest
类的方法图
代码示例
/*** 测试类,实现各种打印机打印*/
public class PrinterTest {public static void main(String[] args) {// 创建针式打印机对象并打印Printer dprinter = new DotMatrixPrinter();dprinter.print();// 创建喷墨打印机对象并打印Printer iprinter = new InkpetPrinter();iprinter.print();// 创建激光打印机对象并打印Printer lprinter = new LaserPrinter();lprinter.print();}
}/*** 抽象打印机类*/
abstract class Printer {/** 抽象方法,实现打印 */public abstract void print();
}/*** 喷墨打印机类*/
class InkpetPrinter extends Printer {/** 打印 */public void print() {System.out.println("打印效果介于针式和激光打印机之间");}
}/*** 针式打印机类*/
class DotMatrixPrinter extends Printer {/** 打印 */public void print() {System.out.println("打印速度慢,效果差,噪音高");}
}/*** 激光打印机类*/
class LaserPrinter extends Printer {/** 打印 */public void print() {System.out.println("打印速度快,噪音小,效果好");}
}
运行截图