In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the example analysis of the implementation interface in Java, which has a certain reference value. Interested friends can refer to it. I hope you will learn a lot after reading this article.
Interface is a very useful concept that can assist us in abstract thinking. In real life, when we think of an appliance, we often think of the functional interface of the appliance. For example, when we think of the possibility of adding water and drinking water, we think of the material and price of the cup. In other words, to a certain extent, the interface of the appliance is the same as the appliance itself. Internal details are discarded in the process of thinking.
In the encapsulation mechanism of public and private, we actually define both classes and interfaces, and classes and interfaces are mixed together. Java also provides the syntax of interface. This syntax separates the interface from the specific definition of the class to form an independent body.
1 、 interface
Take a cup as an example, define the interface of a cup:
Interface Cup {void addWater (int w); void drinkWater (int w);}
The stereotype of two methods is defined in the interface Cup: addWater () and drinkWater (). The prototype of a method specifies the method name, parameter list, and return type. The prototype can tell the outside world how to use these methods.
In interface, we should pay attention to:
There is no need to define the body of the method
There is no need to explain the visibility of the method
In particular, the second point is that the method in interface defaults to public. As we mentioned in encapsulation and interface, the public method of a class constitutes the interface. Therefore, all methods that appear in interface default to public.
We can implement the interface in the definition of a class, such as the following MusicCup (a cup that 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 use the implements keyword to implement interface. Once an interface is implemented in a class, all methods of interface (addWater () and drinkWater ()) must be defined in the class. The methods in the class need to match the method prototypes in interface. Otherwise, Java will report an error.
Other public methods that are not mentioned by interface can be defined in the class. That is, interface specifies a minimum interface that must be implemented. For example, the following waterContent () method does not specify a 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;} 2, the meaning of separating interfaces
We used interface, but this interface does not reduce the amount of work we need to define classes. We still have to write the class concretely as before. We have to be even more careful not to violate the rules of interface. In that case, why do we use interface?
In fact, interface is like an industry standard. A factory (class) can adopt industry standards (implement interface) or not. However, a product that adopts industry standards will have the following benefits:
Higher quality: cups without water function are not up to standard.
Easier to popularize: like the USB interface on a computer, downstream products can be more easily connected.
If we already have a Java program, it is used to deal with objects that conform to the Cup interface, such as leading children to drink water. So, as long as we are sure that we have implemented the Cup interface to the child's cup (object), we can ensure that the child can drink water. As for how the cup (object) specifically defines the action of drinking water, we can leave it to the appropriate class to decide (such as drinking water through a straw or opening a sip).
Interface is a very important concept in computer science. For example, any operating system that provides UNIX interface can be called UNIX system. Linux,Mac OS,Solaris are UNIX systems that provide similar interfaces. However, the specific implementation (source code) of each system is different. Linux is open source, and you can look at every line of code, but you still don't know how to write a Solaris system.
Same UNIX interface:
3. Implement multiple interfaces
A class can implement more than one interface. For example, we have the following interface:
Interface MusicPlayer {void play ();}
Let's consider the MusicCup class again. MusicCup can be seen as a mixture of a player and a cup.
Therefore, MusicCup should have two sets of interfaces, that is, to implement both 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...la");} private int water = 0;}
Finally, you can try to put the interface and the class definition in this article in the same file, and write the Test class to run it.
Thank you for reading this article carefully. I hope the article "sample Analysis of implementation Interface in Java" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.