In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "Java object-oriented how to use polymorphism", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "Java object-oriented how to use polymorphism" bar!
Catalogue
I. Preface
Second, what is polymorphism?
Third, the conditions for the realization of polymorphism
IV. Characteristics of polymorphic access
1. Let's build a service package to put the Animal class
two。 Then create an impl package under the servic package, and delegate the Cat class to the package
3. We are building a controller package in which we can build an animal test class.
4. When we're done, as soon as we run the program,
4.1 Why is there a difference between the two?
5. What are the advantages and disadvantages of polymorphism?
6. Why build the package separately
I. Preface
Now that we've learned and learned about the use of inheritance, let's learn about polymorphism, one of the three major object-oriented.
Polymorphism enriches java object-oriented, so it is very important to learn polymorphism well.
Second, what is polymorphism?
In fact, this problem is obvious, literally, it is also a variety of states, in fact, yes, polymorphism is in
Different situations show different conditions, which are called polymorphism.
For example, we can say,? It's a fruit, so to speak. Yes? (Huaqiang Buy?)
It's an animal, so to speak, huh? Yes? (some people may ask, what is this?)
? Here watermelons and cattle have two different states, and this is polymorphism.
Third, the conditions for the realization of polymorphism
? ① has (inheritance / implementation) relationship
② has a way to rewrite
③ has (parent class / interface) references to (subclass / implementation) objects
Interpretation:
? ① has (inheritance / implementation) relationship (indicates that there is a subclass parent class or interface implementation)
② has method overrides (overriding abstract methods, or overriding methods in the parent class)
③ has (parent class / interface) reference to (subclass / implementation) object (indicating parent class / interface object name = new subclass / implementation)
IV. Characteristics of polymorphic access
? Member method: compile on the left, execute on the right
? Member variables: compile to the left, execution to the left
Interpretation: ① 's so-called compilation looks at the left, that is, on the syntax issue, look at the left side of new (parent class or interface)
Is there any such method or variable? if so, it will be compiled and passed, otherwise it will not pass.
② execution is the final result. For example, when executing in a method, looking to the right is to the right of new.
That is, when the object is called, when both the subclass and the parent class have this method, the result is the method in the subclass.
The interface is not covered in this article, so this time we use inherited class to implement polymorphism.
1. We build a service package for Animal class package service; public class Animal {public String name= "animals"; public void eat () {System.out.println ("animals eat");} public void sleep () {System.out.println ("animals want");}} 2. Then create an impl package under the servic package, and delegate the Cat class to the package
Because we want to inherit the Animal class, and Animal is no longer in the same package with it, we need to guide the package (the guide package is
Tell the program where the one I want to use is.
Package service.impl; import service.Animal;// guide package public class Cat extends Animal {public String name= "cat"; @ Override public void eat () {/ / override parent method System.out.println ("eat?");} public void play () {System.out.println ("play with hairball");}} 3. We are building a controller package in which we build an animal test class import service.Animal;// guide package import service.impl.Cat; public class AnimalController {public static void main (String [] args) {/ / polymorphic creation object Animal a=new Cat (); / / parent class reference refers to child class object a.eat (); a.sleep (); System.out.println ("name:" + a.name) }} 4. When we're done, as soon as we run the program,
As shown in the figure:
If you look at the results, you can see that the compilation of the method is seen as the left, the execution is seen as the right, and the compilation of members is seen as the left.
The execution looks on the left, that is, in the method, it is dominated by the subclass method, and in the variable, it is dominated by the variable of the parent class.
When the parent class has the method and the subclass does not have the method, the result is the method of the parent class.
4.1 Why is there a difference between the two?
In fact, the main reason is that methods are overridden, while member variables are not overridden (variables cannot be overridden).
5. What are the advantages and disadvantages of polymorphism?
? The advantage of polymorphism is that it improves the extensibility, and the subclass can be extended on the basis of the parent class.
? The disadvantage of polymorphism is that you cannot access member variables and member methods in subclasses.
Solution: later involved
In fact, as long as it is implemented through inheritance, the subclass contains the most methods, and at this time we use the subclass
By creating an object, you can access all members (except private members that are not subclasses)
As shown in the figure:
Import service.Animal;import service.impl.Cat; public class AnimalController {public static void main (String [] args) {Cat c=new Cat (); / / create objects through subclasses c.eat (); c.sleep (); c.play (); System.out.println ("name:" + c.name);}} VI. Why build packages separately
? Some people do not understand, built under the same package, there is no need to guide the package, it is not so troublesome, indeed, this is a bit of a problem
None, but we should form this good habit of opening packages, so that when there is a lot of code, it is easy to
Troubleshoot errors. Because writing code is always on the spur of the moment, looking for bug is full of bitterness. In order to prevent this situation
We should form the good habit of building packages separately.
Thank you for reading, the above is the content of "Java object-oriented how to use polymorphism". After the study of this article, I believe you have a deeper understanding of how to use polymorphism in Java object-oriented, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.