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()); } }
原文:http://www.cnblogs.com/sflik/p/4543039.html