import com.sun.management.UnixOperatingSystemMXBean; /** * Created by xabcd on 2019/2/16. */ interface java_usb { public void start(); public void stop(); } class MoveDisk implements java_usb { public void start() { System.out.println("MoveDisk start..."); } public void stop() { System.out.println("MoveDisk stop..."); } } class Mp3 implements java_usb { public void start() { System.out.println("Mp3 start..."); } public void stop() { System.out.println("Mp3 stop..."); } } class Computer { public void work(java_usb u) { u.start(); u.stop(); } }
/** * Created by xabcd on 2019/2/16. */ public class test_usb { public static void main(String args[]) { new Computer().work(new MoveDisk()); new Computer().work(new Mp3()); } } 结果: MoveDisk start... MoveDisk stop... Mp3 start... Mp3 stop...