What is an interface and how to understand it

Posted by methodlessman on Sun, 05 Dec 2021 09:34:01 +0100

The interface in Java is actually a set of "standards" or "specifications". For example, the interface is used to realize [computer management of mouse and keyboard using USB interface]. There is no subclass and parent relationship between mouse and keyboard and the interface, but both mouse and keyboard should be inserted into the computer through USB interface. If there is no unified specification, So how should the computer recognize it? Therefore, a "standard" is needed to uniformly restrict the use rules of mouse and keyboard. Here is a small example:

 

 1 public interface USB {
 2     void work();
 3 }
 4 
 5 class Keyboard implements USB{
 6     @Override
 7     public void work() {
 8         System.out.println("Here is the keyboard implementation USB Method of");
 9     }
10 }
11 
12 class Mouse implements USB{
13     @Override
14     public void work() {
15         System.out.println("Here is the mouse implementation USB Method of");
16     }
17 }
18 
19 class computer {
20     void computerStart(){
21         System.out.println("--Computer boot");
22     }
23 
24     void computerStop(){
25         System.out.println("--Computer off");
26     }
27     void useUSB(USB usb){
28         USB u = usb;
29         u.work();
30     }
31 }
32 
33 class TestUsB {
34     public static void main(String[] args) {
35         computer computer = new computer();
36         computer.computerStart();
37         computer.useUSB(new Keyboard());  
38         computer.computerStop();
39 
40     }
41 }

 

Here, a USB interface standard is uniformly defined. This standard defines a method work(). This USB interface is the USB interface on the computer. If any device wants to connect to my computer through this USB interface, it must follow my rules. This rule is the work method, But why is there only a method defined here without specific implementation? Because the USB interface of the computer does not know who the device to be inserted is, a "contract" is written in advance and placed here. The contract is blank. If a device wants to connect to the computer through "I", take away the agreement and sign an agreement on it.

1 public interface USB {  
2   void work();
3 }

 

For example, if the keyboard wants to connect to the computer through USB, the keyboard needs to take the "agreement" and write down the information on it, indicating "abide by the agreement", so it is written as: class Keyboard follows the USB agreement through the keyword implements, and a USB protocol(   work method) take it and write down the agreement on it. This is the method of implementing the interface and then rewriting the interface.

1 class Keyboard implements USB{
2     @Override
3     public void work() {
4         System.out.println("Here is the keyboard implementation USB Method of");
5     }
6 }

 

For example, here is another mouse that needs to be connected to the computer through USB, so it also needs to be connected through USB   implements keyword to follow the USB agreement, then take a USB agreement and sign the agreement on it (I just print a sentence to distinguish which device)

1 class Mouse implements USB{
2     @Override
3     public void work() {
4         System.out.println("Here is the mouse implementation USB Method of");
5     }
6 }

 

Now we are testing that two devices are connected to the computer through USB. Since we want to use the computer, we must establish a computer entity, that is, a new computer. The computer itself also has many functions, such as turning on the computer, turning off the computer, a series of operations on the computer, etc. another function is to use the function of USB, because we need to connect external devices through this USB, Therefore, a method is defined here: useUSB(USB usb). The parameters of this method receive an object of USB type. Because the computer needs to connect many devices through this excuse, it does not know which one to connect before it is connected, so an object of USB standard is defined in advance, because all devices that can connect to the computer comply with this standard, Those who do not follow this standard (those who do not sign an agreement on the USB standard) cannot connect to the computer.

 1 class computer {
 2     void computerStart(){
 3         System.out.println("--Computer boot");
 4     }
 5 
 6     void computerStop(){
 7         System.out.println("--Computer off");
 8     }
 9     void useUSB(USB usb){
10         USB u = usb;
11         u.work();
12     }
13 }

 

The computer, mouse, keyboard and interface standards are also defined. Next, the user will use the computer. See whether the computer can use the mouse or keyboard through the interface standards we just defined. Look at the following code. First, to use the computer, you need to create a computer entity through the new keyword before you can really use it, By calling computer.computerStart(); Method, and computer.computerStop(); The method is to test these two functions of the computer. You can print the "computer startup" and "computer shutdown" functions of the computer. Next, we use the computer.useUSB(new Keyboard()) of the computer; This function is used to insert a (that is, a new) device.

1 class TestUsB {
2     public static void main(String[] args) {
3         computer computer = new computer();
4         computer.computerStart();
5         computer.useUSB(new Keyboard());  /
6         computer.computerStop();
7     }
8 }

 

The above code is to test the keyboard, that is, insert a keyboard, and then call the useusb (USB) method of the computer, that is, use the USB method. When inserting a keyboard, a keyboard object will be received here. Because the keyboard has "signed the USB standard" on it, USB is recognized and recognized, so after seeing it, Immediately call the agreement signed before the keyboard, and call the functions of the keyboard through the work method of the keyboard. Then the function of using the keyboard is realized here. If a new mouse object is passed in the test class, the USB standard here will recognize that "this is the mouse, call the method of the mouse". This is actually a polymorphic use.

1    void useUSB(USB usb){
2         USB u = usb;
3         u.work();
4     }

 

The final printout is shown in the figure below:

 

The above is a small example to illustrate the use of the interface in JAVA. If you think it is helpful for your understanding, please send it to help more people understand the interface

 

Topics: Java