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/01 Report--
This article introduces the relevant knowledge of "case code analysis of upward transformation of 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. Description
The upward transformation is to reference a subclass to a parent class reference, that is, the parent class reference refers to the object of the subclass, that is, the parent class object = the subclass instance.
At this point, the method called through the parent class reference variable is that the subclass overrides or inherits the parent class's method, not the parent class's method. However, the property called is still the property of the parent class.
2. Instance class Animal {public String name; public void eat () {System.out.println (this.name + "eating");}} class Cat extends Animal {} public class Test extends TestDemo {public static void main (String [] args) {/ / parent reference refers to the object referenced by the child reference Cat cat = new Cat (); Animal animal = cat;// upward transformation}
Expansion of knowledge points:
Java allows the subclass object to be assigned to the reference variable of the parent class without any cast, and the system completes it automatically. The upward transformation comes from the bottom-up inheritance relationship, the subclass inherits the parent class, and the subclass is a special parent class, so the operation of upward transformation is reasonable.
Here is a simple code to try to understand the concepts and benefits of upward transformation.
Package com.my.pac14;/** * @ auther Summerday * / public class DynamicBinding {/ / Object is the superclass of all classes. Depending on the upward transformation, this method can accept any type of object public static void test (Object x) {System.out.println (x.toString ());} public static void main (String [] args) {test (new PrimaryStudent ()); / / Student test (new Student ()) / / Student test (new Person ()); / / Person test (new Object ()); / / java.lang.Object@1b6d3586}} class Person extends Object {@ Override public String toString () {return "Person";}} class Student extends Person {@ Override public String toString () {return "Student" }} class PrimaryStudent extends Student {} "upward transformation example code analysis of Java polymorphism" ends 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.