usb接口程序设计
package duixiang4;
public class Exampel12 {
public static void main(String[] args) {
Computer c=new Computer();
Mouse m=new Mouse();
c.add(m);
c.add(new Mic());
c.add(new KeyBoard());
c.powerOn();
System.out.println();
c.poweOff();
}
}
interface abc{
void trunOn();
void turnOff();
}
class Mouse implements abc{
public void trunOn() {
System.out.println("鼠标启动了");
}
public void turnOff() {
System.out.println("鼠标关闭了");
}
}
class KeyBoard implements abc{
public void trunOn() {
System.out.println("键盘启动了");
}
public void turnOff() {
System.out.println("键盘关闭了");
}
}
class Mic implements abc{
public void trunOn() {
System.out.println("麦克风启动了");
}
public void turnOff() {
System.out.println("麦克风关闭了");
}
}
class Computer{
private abc[] usbArr=new abc[4];
public void add(abc usb) {
for(int i=0;i<usbArr.length;i++) {
if(usbArr[i]==null) {
usbArr[i]=usb;
break;
}
}
}
public void powerOn() {
for(int i=0;i<usbArr.length;i++) {
if(usbArr[i]!=null) {
usbArr[i].trunOn();
}
}
System.out.println("开机成功");
}
public void poweOff() {
for(int i=0;i<usbArr.length;i++) {
if(usbArr[i]!=null) {
usbArr[i].turnOff();
}
}
System.out.println("关机成功");
}
}