1 interface USB { 2 public abstract void open(); 3 public abstract void close(); 4 } 5 6 public class BookPC { 7 8 9 public static void main(String[] args) { 10 useUSB(new Mouse()); 11 12 } 13 14 public static void useUSB(USB u) { //接口类型的引用,用于接收(指向)接口的对象子类 15 u.open(); 16 u.close(); 17 } 18 } 19 20 class Mouse implements USB { 21 public void open() { 22 System.out.println("open Mouse.."); 23 } 24 public void close() { 25 System.out.println("close Mouse.."); 26 } 27 }
时间: 2024-10-01 02:15:45