interface USB { void start(); void stop(); } class USBDisk implements USB { @Override public void start() { System.out.println("U盘插入"); } @Override public void stop() { System.out.println("U盘拔出"); } } class Print implements USB { @Override public void start() { System.out.println("打开打印机"); } @Override public void stop() { System.out.println("关闭打印机"); } } class PC{ public static void work(USB usb){ usb.start(); System.out.println("检测到有设备接入"); usb.stop(); } } public class InterfaceDemo { public static void main(String args[]){ PC pc = new PC(); pc.work(new USBDisk()); pc.work(new Print()); } }
时间: 2024-10-25 19:47:51