package com;
public class Demo02 {
public static void main(String[] args) {
NoteBook nb = new NoteBook();
nb.startUp();
nb.useUSB(new Mouse());
nb.useUSB(new KeyBoard());
nb.shutDown();
}
}
interface USB {
public void start();
public void close();
}
class NoteBook {
public void startUp() {
System.out.println("开机");
}
public void shutDown() {
System.out.println("关机");
}
public void useUSB(USB usb) {//USB usb = new KeyBoard();多态格式 父接口引用指向子类对象
usb.start();
usb.close();
}
}
class KeyBoard implements USB {
@Override
public void start() {
System.out.println("连接键盘的USB");
}
@Override
public void close() {
System.out.println("断开键盘的USB");
}
}
class Mouse implements USB {
@Override
public void start() {
System.out.println("连接鼠标的USB");
}
@Override
public void close() {
System.out.println("断开鼠标的USB");
}
}
抽象类 接口 练习题
最新推荐文章于 2023-12-08 09:56:03 发布