Home  >  Article  >  Java  >  How to implement the function of simulating USB interface in java

How to implement the function of simulating USB interface in java

WBOY
WBOYforward
2023-05-26 18:07:222640browse

1 Question

Simulate the function of USB interface
We often use USB interface when using computers. Mouse, keyboard, etc. are all devices with USB interface. We only need to connect the mouse, These peripherals with USB interface can be used normally by plugging the keyboard into the USB interface of the computer.
Through the interface-oriented programming design concept of JAVA program, the process of using the USB interface to drive the mouse and keyboard is simulated.

Requirements:
① Define the USB interface, which has the turnOn() method to start the USB device, and the turnOff() method to close the device;
② Define the Mouse, KeyBoard, and MicroPhone microphone The class implements the USB interface;
③ Define the computer class
powerOn() to turn on the computer, all USB devices need to be loaded before starting;
powerOff() to shut down, all the USB devices need to be ejected before turning off the computer.
Note: The mouse and other devices do not need to be plugged in when the computer is turned on

2 Idea analysis

2.1 Write an interface USB, which contains the turnOn method and the turnOff method

2.2 Write the Mouse (mouse), KeyBoard (keyboard), and MicroPhone (microphone) classes to implement the USB interface

2.3 Rewrite turnOn and turnOff to print the corresponding object information

2.4 The Computer class establishes an interface Array, used to store the instantiated objects of each subclass that implements the interface

2.5 Create a single-time addition device method addUSB (USB usb) in the Computer class, so that each time this method is called, the computer A device will be plugged in where the interface array object is not null

2.6 PowerOn in Computer traverses all USB interfaces. If the interface object is not null, the overridden turnOn method of the device is called. , when all are traversed, it will print that the computer started successfully.

2.7 PowerOff in Computer traverses all USB interfaces. If the interface object is not null, the overridden turnOff method of the device is called, and then the interface object is assigned null. When the traversal is completed All will be printed after the computer shuts down successfully.

2.8 If there are no devices that need to be loaded or ejected before starting up or before shutting down, it will output that there are no devices that need to be loaded before starting up or there are no devices that need to be ejected before shutting down

3 Code Implementation

USB interface

package Work4;
public interface USB {
    //接口里面的方法都是抽象方法,那个public abstract可以省略不写的!!!
    void turnOn();
    void turnOff();
}

KeyBoard class

package Work4;

public class KeyBoard implements USB{
    private String name;

    public KeyBoard() {
    }

    public KeyBoard(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void turnOn() {
        System.out.println(name+"键盘加载了");
    }

    @Override
    public void turnOff() {
        System.out.println(name+"键盘弹出了");
    }
}

MicroPhone class

package Work4;

public class MicroPhone implements USB{
    private String name;
    public MicroPhone() {
    }

    public MicroPhone(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void turnOn() {
        System.out.println(name+"麦克风加载了");
    }

    @Override
    public void turnOff() {
        System.out.println(name+"麦克风弹出了");
    }

}

Mouse class

package Work4;

public class Mouse implements USB{
    private String name;

    public Mouse() {
    }

    public Mouse(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void turnOn() {
        System.out.println(name+"鼠标加载了");
    }

    @Override
    public void turnOff() {
        System.out.println(name+"鼠标弹出了");
    }
}

Computer class

package Work4;
public class Computer {
    //弄一个USB接口,里面的默认都为null
    private USB[] usbs=new USB[4];
    public Computer() {
    }
    //每次只能增加一个设备
    public void addUSB(USB usb){
        for (int i = 0; i <usbs.length; i++) {
            //如果为空,则代表可以插入设备,否则就插入不了的
            if(usbs[i]==null){
                usbs[i]=usb;
                break;
            }
        }
    }
    public void powerOn(){
        boolean flag=false;
        //判断是否插入了设备,只要有就为false
        for (int i = 0; i <usbs.length; i++) {
            if(usbs[i]!=null){
                //如果USB接口不为空,则代表有设备,需要启动的
                usbs[i].turnOn();
                flag=true;
            }
        }
        if(!flag){
            System.out.println("开机前没有需要加载的设备");
        }
            System.out.println("开机成功!!!!");
    }
    public void powerOff(){
        boolean flag=false;
        //判断是否还有设备没有设备被弹出,只要有就为false
        for (int i = 0; i <usbs.length; i++) {
            if(usbs[i]!=null){
                //如果USB接口不为空,则代表有设备,关机前需要弹出这些设备的
                usbs[i].turnOff();
                usbs[i]=null;
                flag=true;
                //弹出设备后,对应的接口上面就没有东西了,理应设置为null
            }
        }
        if(!flag){
            System.out.println("关机前没有需要弹出的设备");
        }
            System.out.println("关机成功!!!!");
    }
}

TestComputer

package Work4;
public class TestComputer {
    public static void main(String[] args) {
        Computer c=new Computer();
        c.addUSB(new Mouse("罗技"));
        c.addUSB(new KeyBoard("双飞燕"));
        c.addUSB(new MicroPhone("铁三角"));
        c.powerOn();
        c.powerOff();
        //没有插入设备时测试
        c.powerOn();
        c.powerOff();
    }
}

4 Code running screenshot

How to implement the function of simulating USB interface in java

The above is the detailed content of How to implement the function of simulating USB interface in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete