In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to define and apply Java polymorphism". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. What is polymorphism
Concept: refers to the ability of the same behavior to have different forms or forms of expression.
2. Definition format parent class name object name = new subclass name (); / / or interface name object name = new implementation class name ()
Access method:
(1) access member variables directly through the object name: who is on the left side of the equal sign, who has priority, and does not look up.
(2) access member variables indirectly through member methods: see who the method belongs to and who is preferred, instead of looking up.
3. Polymorphic application package Demo05;public class Demo01Multi {public static void main (String [] args) {Father son = new Son (); System.out.println (son.num); / / parent class: 10 System.out.println ("="); son.showNum (); son.method (); System.out.println ("="); son.methodFather () / / son.methodSon (); / / incorrect spelling}} public class Father {int num = 10; public void showNum () {System.out.println (num);} public void method () {System.out.println ("I'm the father");} public void methodFather () {System.out.println ("parent method");}} public class Son extends Father {int num = 20 @ Override public void showNum () {System.out.println (num);} @ Override public void method () {System.out.println ("I am the father's son");} public void methodSon () {System.out.println ("subclass methods");}}
Print the result
ten
=
twenty
I'm my father's son.
=
Parent method
In polymorphic code, the access rules for member methods are:
If you look at the new, you will give priority to the one who uses it, and if you don't, you will look up.
Formula: compile to the left, run to the right.
Member variables: compile and run to the left.
Member method: compile to the left, run to the right.
4. Type conversion
(1) upward transformation
Polymorphism itself is the process of converting a subclass type up to a parent type, which is the default.
When a parent class reference points to a subclass object, it transitions upward.
Format:
Parent class type variable name = new subclass type ()
Upward transformation must be safe, no problem, and correct. But there is also a drawback: once transformed into a parent class, you cannot invoke the originally specific content of the subclass.
(2) downward transformation
The process of converting a parent type to a subclass type, which is mandatory.
Format:
Subclass type variable name = (subclass type) parent class variable name
If you want to know what subclass is the object referenced by the parent class. You can use the instanceof keyword to check the type of a reference variable.
Object instanceof class name; "how to define and apply polymorphisms of Java" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.