interface Usb {
abstract public void on();
abstract public void off();
}
class Mouse implements Usb {
public void on() {
System.out.println("鼠标上电 23v");
}
public void off() {
System.out.println("鼠标关电 23v");
}
}
class Keyback implements Usb {
public void on() {
System.out.println("键盘上电 223v");
}
public void off() {
System.out.println("键盘关电 223v");
}
}
class Light implements Usb {
public void on() {
System.out.println("灯上电 223v");
}
public void off() {
System.out.println("灯关电 223v");
}
}
class Notebook {
public void shibieUsb(Usb u) {
u.on();
}
public void run(Mouse m, Keyback k, Light l) {
shibieUsb(m);
shibieUsb(k);
shibieUsb(l);
System.out.println("电脑可以运行起来了");
}
}
class Demo1 {
public static void main(String[] args) {
Notebook n = new Notebook();
n.run(new Mouse(), new Keyback(), new Light());
}
}
这应征了多态的三个特性:
引用名是父类或者接口,子类对它们进行了重写。父类,接口的引用指向子类。