USB

Interface programming

1.

Task description

It’s usually on the computer people use

USB

Interface, mouse, keyboard, microphone, etc can be connected to
USB
Interface. When the computer starts up, these devices start up with it; When the computer shuts down, so do the devices. Mouse, keyboard, microphone, etc
USB
After the interface devices are started, the computer can be started successfully; When these
USB
The computer was successfully shut down after all the devices were shut down. Write a
USB
Interface program, simulate the described computer startup and shutdown process.

2.

Task goal

(

1

) Learn to analyze.”
USB
Interface programming “task implementation logic

(

2

) can be completed independently.
USB
Interface programming “source code preparation, compilation and operation

(

3

) master
Java
The concept and use of interfaces

3.

Implementation approach

(

1

) From the task description and the analysis of the running results of the program, it can be seen that the objects designed in this task are
USB
Interface, mouse, keyboard, microphone and computer. To implement this program, you need to define these objects accordingly.

(

2

) First, the mouse, keyboard, microphone and so on
USB
A device can only be used if it is plugged into an interface, so you need to define one first
USB
Interface. As a result of these
USB
The device needs to be started with the computer and shut down with the computer, so you need to define ways to start and shut down the device in the interface.

(

3

) after writing the interface, the next step is to write the implementation class of the interface mouse, keyboard, microphone, in the implementation class to implement the start and close methods of these devices.

(

4

Since these devices are used in a computer, you need to write a computer class next. It’s in the computer
USB
After the slot is inserted
USB
Interface, each time a new device is inserted, the computer will install the device’s driver. After the driver is installed, the device can be used properly, so you need to write one in this class
USB
Slot and installation
USB
Method of equipment. At the same time, the computer should want to shut down, but also define the method of starting and shutting down.

(

5)

Finally, write the test class, instantiate the computer object, and add to it
USB
Device, run to view results.

Personal code is as follows:
USB

class

public interface USB {
void turnOn();
void turnOff();
}
The implementation class of the interface

public class Mouse implements USB {
@Override
public void turnOn() {
// TODO Auto-generated method stub
System.out.println(”

Mouse up
“);

}
@Override
public void turnOff() {
// TODO Auto-generated method stub
System.out.println(”

Mouse off
“);

}
}
public class KeyBoard implements USB{
@Override
public void turnOn() {
// TODO Auto-generated method stub
System.out.println(”

The keyboard is up
“);

}
@Override
public void turnOff() {
// TODO Auto-generated method stub
System.out.println(”

The keyboard is off
“);

}
}
public class Microphone implements USB{
@Override
public void turnOn() {
// TODO Auto-generated method stub
System.out.println(”

The microphone is on
“);

}
@Override
public void turnOff() {
// TODO Auto-generated method stub
System.out.println(”

The microphone is off.
“);

}
}
The computer class

public class Computer {
private USB[] usbArr = new USB[4];
public void add(USB usb) {
for(int i=0; i
if(usbArr
==null) {
usbArr=usb;
break;
}
}
}
public void powerOn() {
for(int i=0; i<usbArr.length; i++) {
if(usbArr! =null) {
usbArr.turnOn();
}
}
System.out.println(”

Computer started successfully
“);

}
public void powerOff() {
for(int i=0; i<usbArr.length; i++) {
if(usbArr! =null) {
usbArr.turnOff();
}
}
System.out.println(”

Computer shutdown succeeded
“);

}
}

The test class

public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Computer p = new Computer();
p.add(new Mouse());
p.add(new KeyBoard());

Function (){// forex returns http://www.fx61.com/

p.add(new Microphone());
p.powerOn();
System.out.println(“—————–“);
p.powerOff();
}
}

Console output

Mouse up
The keyboard is up
The microphone is on
Computer started successfully
—————–
Mouse off
The keyboard is off
The microphone is off.
Computer shutdown succeeded

More technical information can be obtained from itheimaGZ