In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
What this article shares with you is what the strange overload of Java's function is, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
As we all know, classes in Java can only inherit one parent class, but can implement multiple interfaces. The reason for this is said to be that the concept of multiple inheritance is confusing. However, there will be more confusion in the multi-interface implementation.
1. What if a class implements two interfaces, but there are functions with the same signature in the two interfaces?
In fact, there can only be one function with a consistent signature in the same function. As follows:
Interface ClickListener {void fire ();} interface TouchListener {void fire ();} class Button implements ClickListener, TouchListener {@ Override public void fire () {/ /...}}
Calls to fire from both Listener point to the same function.
two。 On the basis of 1, if the parent class of Button already has a function with the same signature. As follows:
Interface ClickListener {void fire ();} interface TouchListener {void fire ();} class ButtonBase {public void fire () {/ /...}} class Button extends ButtonBase implements ClickListener, TouchListener {}
Then there may be no functions in the Button at this time. Both Listener calls to fire point to functions in ButtonBase. Is that weird?
Compared with the "complex" multiple inheritance in C++, this strange practice has no idea which Listener triggers the call in the called function, and cannot be handled differently according to different sources. Even more perversely, if functions in two Listener only return different values-- sorry, sadly-- you can't implement both interfaces at the same time-- because the compiler doesn't support it.
However, this strange practice also has its practical uses, as shown in the following example:
/ * public interface * / public interface Animal {void run ();} / * internal class * / private class Dog {@ Override void run () {/ /...}} / * public interface * / public FlyAnimal extends Animal {void fly ();} / * internal class * / private class FlyDog extends Dog implements FlyAnimal {@ Override void fly () {/ /...}}
All I can say is that Java is too ferocious.
These are the strange overloads of Java's functions, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.