Author: vamei Source: http://www.cnblogs.com/vamei welcome reprint, please also keep this statement. Thank you!
We use the Public and Private keywords in encapsulation and interfaces to encapsulate object members. After encapsulation, the product hides internal details and only provides them to users.Interface).
An interface is a very useful concept that assists us in abstract thinking. In real life, when we think of an appliance, we often think of its functional interface. For example, the possibility of adding water and drinking water to a cup is higher than the material and price of the cup. That is to say, we equate the interface of the appliance with the concept of the appliance to a certain extent, while the internal details are abandoned in the process of thinking.
A cup in mind
In the encapsulation mechanism of public and private, we define both classes and interfaces. Java also providesInterfaceThis syntax. This syntax willInterfaceIt is separated from the specific definition of the class to form an independent entity.
Interface
Take a cup as an example to define a cup interface:
InterfaceCup {VoidAddwater (IntW );VoidDrinkwater (IntW );}
The cup interface defines the prototype of two methods (stereotype): addwater () and Drinkwater (). The prototype of a method specifies the method name, parameter list, and return type. A prototype can tell external users how to use these methods.
In the interface, we
- The subject of the method does not need to be defined.
- No need to describe method visibility
Note that the method in interface is public by default. As we mentioned in encapsulation and interface, the public method of a class constitutes an interface. Therefore, all methods that appear in the interface areThe default value is public..
We can implement interfaces in the definition of a class, such as the following musiccup (which can play music ):
Class Musiccup Implements Cup { Public Void Addwater ( Int W ){ This . Water = This . Water + W ;} Public Void Drinkwater ( Int W ){ This . Water = This . Water-W ;} Private Int Water = 0 ;}
We useImplementsKeyword to implement the interface. Once an interface is implemented in the class, all methods (addwater () and Drinkwater () of the interface must be defined in the class ()). The methods in the class must be consistent with the method prototype in the interface. Otherwise, Java reports an error.
In the class, you can define other public methods not mentioned by the interface. That is, the interface specifies the minimum interface that must be implemented. For example, the following watercontent () method does not specify the prototype in the cup interface:
Class Musiccup Implements Cup { Public Void Addwater ( Int W ){ This . Water = This . Water + W ;} Public Void Drinkwater ( Int W ){ This . Water = This . Water- W ;} Public Int Watercontent (){ Return This . Water ;} Private Int Water = 0 ;}
Significance of separating Interfaces
We use the interface, but this interface does not reduce the workload when we define the class. We still need to write classes as before. We should be more careful not to violate the interface rules. In this case, why should we use interfaces?
In fact, the interface is likeIndustry standards. A factory (class) can adopt industrial standards (implement Interface) or not. However, a product that adopts industry standards will have the following benefits:
- Higher quality: the cup without the function of adding water does not meet the standards.
- Easier promotion: Just like USB interfaces on computers, downstream products can be more easily connected.
If we already have a JavaProgramTo process objects that match the cup interface, such as drinking water. Then, as long as we confirm, we have implemented a cup interface for the child's cup (object) to ensure that the child can perform the drinking water action. As for how the cup (object) defines the action of drinking water, we can leave it to the appropriate class (for example, using a straw to drink water or opening a mouth to drink water ).
Interfaces are an important concept in computer science. For example, any operating system that provides UNIX interfaces can be called a UNIX system. Linux, Mac OS, and Solaris are all UNIX systems that provide similar interfaces. However, the specific implementation of each system (Source code. Linux is open-source. You can view every line of it.CodeBut you still don't know how to write a Solaris system.
Same UNIX Interface
Implement multiple interfaces
A class can implement more than one interface. For example, we have the following interface:
InterfaceMusicplayer {VoidPlay ();}
Let's look at the musiccup class. Musiccup can be seen as a mixture of players and cups.
Therefore, musiccup should have two sets of interfaces: The musicplayer interface and the cup interface:
Class Musiccup Implements Musicplayer, cup { Public Void Addwater ( Int W ){ This . Water = This . Water + W ;} Public Void Drinkwater ( Int W ){ This . Water = This . Water-W ;} Public Void Play () {system. Out. println ( "La... la" );}
Private Int Water = 0 ;}
Finally, you can try to put the interface and class definitions in this article in the same file, write the test class, and run it.
Summary
Interface, method stereotype, public
Implements Interface
Implements interface1, interface2
Welcome to continue reading the "Java quick tutorial" SeriesArticle