javaUSB接口
2023-04-21 10:05:22
package TestUSB;
interface USB{void turnOn();void turnOff();}
class Mouse implements USB{public void turnOn() {System.out.println("鼠标启动..";}public void turnOff() {System.out.println("鼠标关闭..";}}
class Keyboard implements USB{public void turnOn() {System.out.println(“键盘启动···”;}public void turnOff() {System.out.println(“键盘关闭……”;}}class Camera implements USB{public void turnOn() {System.out.println(“摄像头启动···”;}public void turnOff() {System.out.println("摄像头关闭..";}}
class Computer{private USB[] usbArr=new USB[4];public void add(USB 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].turnOn();}}System.out.println(”计算机启动成功!=null) {usbArr[i].turnOn();}}System.out.println("计算机开机成功!";}public void powerOff() {for(int i=0;i<usbArr.length;i++) {if(usbArr[i]!=null) {usbArr[i].turnOff();}}System.out.println(”计算机关机成功!");}}
public class TestUSB {public static void main(String[] args) {Computer c=new Computer();c.add(new Mouse());c.add(new Keyboard());c.add(new Camera());c.powerOn();System.out.println(“计算机运行”);c.powerOff();}}