Java实现USB接口程序设计

2021-03-07 20:30

阅读:488

标签:pen   dell   false   main   bar   oar   rri   实现   lan   

USB接口程序设计

USB.java

public abstract class USB {
    boolean stat=false;
    abstract void  open();
    abstract void close();
}

Mouse.java

public class Mouse extends USB{
    @Override
    public void open() {
        stat=true;
        System.out.println("鼠标启动了");
    }

    @Override
    public void close() {
        stat=false;
        System.out.println("鼠标关闭了");
    }
}

Keyboard.java

public class Keyboard extends USB{
    @Override
    public void open() {
        stat=true;
        System.out.println("键盘启动了");
    }

    @Override
    public void close() {
        stat=true;
        System.out.println("键盘关闭了");
    }
}

Microphone.java

public class Microphone extends USB{
    @Override
    public void open() {
        stat=true;
        System.out.println("麦克风启动了");
    }

    @Override
    public void close() {
        stat=true;
        System.out.println("麦克风关闭了");
    }
}

Computer.java

public class Computer {
    public USB[] getUsbArr() {
        return UsbArr;
    }

    public void setUsbArr(USB[] usbArr) {
        UsbArr = usbArr;
    }

    private USB[] UsbArr=new USB[3];
   //遍历所有接口,如果有接口未打开,则把接口插上
   public void connect()
   {
       for (USB usb : UsbArr) {
           if (!usb.stat) {
               usb.open();
           }
       }
   }
    //遍历所有接口,如果有接口未打开,则把接口插上
    public void disconnect()
    {
        for (USB usb : UsbArr) {
            if (usb.stat) {
                usb.close();
            }
        }
    }
    //开电脑
    public void openComputer()
    {
        connect();
        System.out.println("电脑已打开");
    }
    public void closeComputer()
    {
        disconnect();
        System.out.println("电脑已关闭");
    }
}

Test.java

public class Test {
    public static void main(String[] args) {
        Computer dell=new Computer();
        dell.setUsbArr(new USB[]{new Mouse(),new Keyboard(),new Microphone()});
        dell.openComputer();
        dell.closeComputer();

    }
}

Java实现USB接口程序设计

标签:pen   dell   false   main   bar   oar   rri   实现   lan   

原文地址:https://www.cnblogs.com/MirandaYuen/p/14257146.html


评论


亲,登录后才可以留言!