Super keyword in Java_ Power node Java college sorting

1、 Super keyword

In the Java class, use super to refer to the components of the parent class and this to refer to the current object. If a class inherits from another class, when we new the instance object of this subclass, there will be a parent object in this subclass object. How to reference the parent object inside? Use super to reference. This refers to the reference of the current object, and super refers to the reference of the parent object in the current object.

Super keyword test

Operation results:

Draw a memory analysis diagram to understand the whole process of program execution

The analysis of any program starts from the first sentence of the main method, so first analyze the first sentence in the main method:

When the program is executed here, a variable CC will be generated in the stack space. It's hard to say what the value in CC is. In a word, we can find the new chlidclass object through this value. Because the subclass chlidclass inherits from the parent class fatherclass, when we create a new subclass object, the subclass object will contain a parent object, and the parent object has its own attribute value. This value member variable is not initialized when it is declared in the fatherclass class, so it is initialized to 0 by default, A member variable (declared in a class) may not be initialized when declared, and the compiler will initialize the member variable automatically, but a local variable (declared in a method) must be initialized when declared, because the compiler will not initialize the local variable automatically, and any variable must be initialized before use.

While the subclass inherits the value attribute of the parent class, it also defines a value attribute separately. Therefore, when we new a subclass object, the object will have two value attributes, one is the value inherited from the parent class, and the other is its own value. The member variable value defined in the subclass is not initialized when declared, so the compiler initializes it to 0 by default. Therefore, after the implementation of the first sentence

Next, execute the second sentence:

When a new object comes out, the object will generate a reference to this, which points to the object itself. If the new object is a subclass object, there will also be a super reference in the subclass object, which points to the parent object in the current object. Therefore, it is equivalent to that there is a this in the program, which points to the object itself, and a super, which points to the parent object in the current object.

Here, call the rewritten f () method, and the first sentence in the method body: "super. F();" It is to let the parent object in this subclass object call its own f () method to change the value of its value attribute. The parent object calls its own f () method by pointing to its reference super. Therefore, after this sentence is executed, the value in the parent object becomes 100. Then execute "value = 200;" The vaule here is the value declared by the subclass object itself, not the value inherited from the parent class. So after this sentence is executed, the value of the subclass object itself becomes 200. The memory layout is shown in the following figure:

The last three sentences in the method body execute the command to print the value value. The first two sentences print the value value of the subclass object, so the printed result is 200. The last sentence prints the value of the parent object in the subclass object, and the printed result is 100. At this point, the whole memory analysis is completed, and the final memory display results are shown above.

The above is the super keyword in Java introduced by Xiaobian_ The power node is sorted out by Java college. I hope it will help you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>