In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article shows you how to analyze java polymorphism through examples. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
First, take a look at a piece of code like this, where the definition of a class is as follows:
Class Parent {public int myValue=100; public void printValue () {System.out.println ("Parent.printValue (), myValue=" + myValue);}} class Child extends Parent {public int myValue=200; public void printValue () {System.out.println ("Child.printValue (), myValue=" + myValue);}}
Let's answer the questions one by one:
1. The running result is as shown in the figure.
two。 For the interpretation of this run result, the first line and the second line are all normal calls, and in the third line, the child is assigned to the output of parent, and we find that the result is 200 and the output statement has changed.
In the fourth sentence, parent.myValue++; was executed but the result did not change. In the fifth line, the parent class is forcibly converted to a subclass and then incremented, the result is 201. 0.
3. For the analysis here, we find that there is no problem for the subclass to assign values to the parent class, that is, the subclass can be converted directly to the parent class. The result of the statement in the fourth line is still 200, indicating that he still calls the Value in the parent class for the + + of myValue, but finally outputs the Value of the subclass. It is stated that after the subclass is assigned to the parent class, it is only partially replaced, not completely covered. In the fifth sentence, the parent class is transformed into a subclass and then carried on + +. In the fourth sentence and the fifth sentence, it can be said that the subclass assigns a value to the parent class, which is not the so-called overlay, but just hides the original thing. Unlike the cast, it becomes a subclass directly.
Finally, this actually tells us that in practical application, we should not set the parent class and subclass to the same source code and member variables, which is very disadvantageous to the programmer's understanding when programming.
The above content is how to analyze java polymorphism through examples. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to 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.