[introduction to Java] Day2 interface

After talking about abstract classes in the previous article, this article mainly explains what is more abstract than abstract classes - interfaces.

What is an interface? Let's take a look at a real chestnut. Our commonly used socket is generally divided into two holes and three holes, so basically no matter what kind of electrical appliance it is, it can be used normally as long as the plug is inserted. Think about it. If there is no such specification, there are more than a dozen different socket holes, and the plug of each electrical appliance is different, so it can't collapse.

Let's start with a chestnut:

This is a simple interface. Use the interface keyword to define the interface.

An interface is a set of behaviors that can belong to any class or structure. It is a specification used to define a program. Any class that implements an interface must implement all methods of the interface, which embodies "if you are..., you must be able to..." My thoughts.

In the interface, there can be no method implementation, that is, all the methods are public abstract, which can only be composed of static variables and no ordinary member variables. It can be described as an abstract integrator. So why use interfaces?

Or continue our previous analogy, any plug produced according to the specifications can obtain power, and although different sockets have different production processes and different quality, it does not affect the normal use of electrical appliances. Electrical appliances do not care about the internal implementation of sockets, so that some bottom details can be shielded and only the use of interface power supply can be exposed.

For software development, calling according to the specification of the interface can obtain the desired function, and the method of implementing the interface according to the specification of the interface can provide the desired function. The meaning of interface lies in abstraction, which can be well decoupled.

Let's look back at the chestnuts in the previous article. We abstract a commodity class from a specific commodity class, so as to realize code reuse and unified management, and reduce the coupling degree of the program. For example, if a sorting method is set to phone class, only phone type objects can be placed in it. If it is set to goods class, then phone, television All objects of computer class can be passed in, which reduces the coupling degree of programs, that is, the dependency between programs.

The interface is a higher-level abstraction, mainly the abstraction of behavior. It can be regarded as a set of specifications or requirements. For example, if you want to drive, you need to take a driver's license first. This driver's license is equivalent to the interface. If you have this driver's license, it means that you have the ability and qualification to drive, Because "if you want to have a driver's license, you must be able to drive". In this way, the traffic police will not embarrass you. You communicate and communicate with the traffic police through the interface of driver's license, rather than persuading him with eloquence. Think about it, if there is no specification of driver's license, you can't miss a driver. You have to test his ability on the spot before you can let him on the road.

Complex system architectures are often divided into many levels. The upper level uses the services provided by the lower level, and the levels communicate through interfaces. The upper level only depends on the interface rather than specific classes for the lower level, because as long as the corresponding interfaces are implemented, the corresponding services can be provided, just like as long as there is a teacher qualification certificate, It means that you have the qualification and ability to be a teacher. What you care about is not your object, but your ability to teach, that is, the services you can provide. When the lower layer needs to change, as long as the interface remains unchanged, the upper layer can not change at all, and even completely replace the lower layer code without the upper layer's knowledge. It is precisely because of the existence of the interface that the coupling between layers is greatly reduced. Just like your USB flash disk, if the old one is broken, you can plug it in directly with a new one, plug and play. The computer communicates with the USB flash disk through the interface.

Well, it's very wordy to say so much. Let's look at the code and practice to know the truth. Let's modify the content of the previous article slightly. The above code defines an isortable interface to compare two objects for subsequent sorting.

Then we define a sort class for sorting. Various types of sorting can be used, such as bubble sorting, selection sorting, quick sorting and Hill sorting. For simplicity, only bubble sorting is used here.

Then implement the isortable interface in the goods class. Use the implements keyword.

Since the goods class implements the isortable interface, all classes inherited from the goods class also implement the interface. Next, we modify the test class for testing.

The output is as follows:

In this way, you can sort from large to small according to the price. How about the interface? It's very simple and convenient to use. In this way, no matter how many subclasses the goods class has, you can sort it with the BubbleSort method of sort. You can also modify or add the method of sort class to sort it according to the rules you want.

In fact, there are already similar interfaces in Java, the comparable interface and the comparator interface. Because generics are used, the code here does not need forced type conversion (and forced type conversion also has certain risks), and many methods can sort the classes that implement the comparable interface, which is great. (manual funny)

In actual use, everyone is often responsible for the development of different modules, and interfaces are usually used for communication between different modules, because if programmer a develops class A1, Class B1 developed by programmer B needs to be used. If Class B1 is called directly, class A1 can only be developed after Class B1 is developed, and if there are any changes to Class B1, It is likely that the code of class A1 needs to be modified, so the coupling degree is very high. If an interface is used, class A1 only needs to accept the parameters of the interface type and can directly call the corresponding methods, while Class B1 only needs to implement the interface. Even if Class B1 changes, as long as the interface remains unchanged, the services it provides to class A1 will not change, so class A1 does not need to be changed, The coupling is reduced.

Now to summarize:

An interface is an abstraction of a group of specific behaviors. It is a specification. It can only have method signatures, but not implementations. All methods are abstract by default, and the access permission can only be public. There can be no ordinary member variables, but static member variables. Interfaces can inherit interfaces. A class can implement one interface or multiple interfaces, The existence of interface can reduce the coupling degree of program and increase the flexibility of program. To quote the great God, interface and implementation are separated, and interface oriented programming.

So far, the interface explanation is completed. Welcome to continue your attention.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>