Parent class and subclass conversion talks (assigning sub-objects to parent class)

xiaoxiao2021-03-06  175

Recently, you have a bit experience with your child objects to the parent class. I want to share with you. But I am limited, please correct and criticize. The words retired, the following is a few small examples, please take a look. Test a father : Public class supClass {public void print () {system.out.println ("This IS Parent Parent PRINT () method" "- That Object" this.tostring ());}} Subclass: Public Class Subclass extends supclass {public static void main (string [] args) {supclass sup = new subclass (); sup.print (); system.out.println ("Object" sup.toString ());}} RESULTS: THIS IS Parent Class Print () Method - At this point, the object is Subclass @ 126b249 This time the object is Subclass @ 126b249 Description: supClass sup = new subclass (); Although the declaration object is a parent class object, but the actual memory space It is a sub-object. The method of inheriting the parent class public void print () is called, and the output is the sub-object name analysis. Conclusion: Objects that are compiled when compiling is a parent class object, but run is a sub-object. The subclass does not rewrite the parent class, then the object calls the method of inheriting the parent class. Test two parent categories: public class supClass {public void print () {system.out.println ("This IS Parent Parent PRINT () method" "- This point Object" this.tostring ());}} Class: Public class subclass extends supClass {public void print () {system.out.println ("this IS subclass print () method" "- this point object" this.tostring ());} public static void Main (String [] args) {supclass sup = new subclass (); sup.print (); system.out.println ("Object" sup.tostring ());}} Results: this IS subclass print () Method - At this point, the object is Subclass @ 126b249 At this point, the object is Subclass @ 126b249 Description: On the basis of the last example, the PRINT () method of the parent class is rewritten. At this time, the call is the subclass PRINT () method.

Conclusion: On the basis of the conclusion of the previous example, I got a conclusion: At this time, the object is indeed a subclass object. If the subclass does not have a method of writing the parent class, the object at this time is called the method of inheriting the parent class; Otherwise, the object calls the subclass method. Question: Is that we can obtain from the above test: assign the sub-object to the parent class object (ie supclass sup = new subclass ()), we get it Sub-class object, ie, SUP is a sub-class object ?????? Test three parent classes: public class supclass {protected class, = "parent class properties"; public void print () {system.out.println ("this is Parental Print () method " " - This time Object " this.tostring ());}} Subclass Extends supclass {protected string classname =" subclass properties "; public void print () { System.out.Println ("this IS subcecline print () method" "- this time" this.tostring ());} public static void main (String [] args) {supclass sup = new subclass ); System.out.println ("At this time:" sup.classname);}}}}: When the property is attribute: parent class properties Description: I am on the first test, give the parent class Added a property className, rewrite this property in the subclass. But when I output the properties of the object, it is the properties of the parent class. Conclusion: Assignment of sub-objects to parent class objects, methods, and attributes and our orthodox The inheritance relationship is very different. Question: At this time, is the subject is a sub-class object, or a parent class object? Start to speculate: I have a few points before speculation: 1. When we get a sub-object, the parent class object The constructor is also executed, that is, some of the necessary information and sub-class objects of the parent class share a memory space, when we rewrite, then we can use Su PER This object of refers to the parent class. 2. The object in Java is not completely object-oriented. The method in the category, it can be said that it is a reference to the method and class to the object to the object. The object call is not a method, but the method in the class. As for the Java, why do you do this, I don't know 3. When the object is loaded into memory, the class first is loaded into memory, and the class should be in memory. As for when the class disappears from memory, I don't know. I think Java must have Your own recycling mechanism is just like a recycling object. 4. Compile and run is a completely different thing. Mainly made when compiling is the type of statement, allocation properties, check the syntax error, etc., load objects Memory (generally used in NEW, reflection is also common), run code execution function, etc. 5. If you read, you and I have not resonated in these points, or if we don't use the same understanding, you will feel it. I am talking in Hu. Maybe you will feel that my expert is too low, I feel that my credibility is low. But I want to say that I haven't divided it, and the people are first. Oh, I am ready to give me Experts have negative value, no walery, we continue. Initially: 1. When we compile supclass sup =

转载请注明原文地址:https://www.9cbs.com/read-128242.html

New Post(0)