The related chapters of this article: the tenth chapter inner class 10.10 Inner class can be overwritten?
When I read the second example code in this section (and the code below), I didn't understand the first "egg.yolk ()" In the output, why it would be the first place to output.
At first I thought it was when I called the constructor of Bigegg.yolk that its parent class called the parent class constructor output, after all, the first line in the main method was to call the constructor of Bigegg, and its constructor called the constructor of Bigegg.yolk, initializing its parent when invoking the Bigegg.yolk constructor. The class Egg.yolk only outputs the "egg.yolk ()". But the second "EGG" is clearly stated in the book. Yolk () "is a subclass Bigegg.yolk initializes the output of its parent class, which confuses me--how does the first" egg.yolk () "Output?
(the sample code in the book, because it is the second example, all the classes are named "Xxx2", where I delete all 2)
/*** Created by Dsa44 on 2018/4/21.*/classegg{classyolk{ Publicyolk () {System.out.println ("Egg.yolk ()"); } Public voidf () {System.out.println ("Egg.yolk.f ()"); } } PrivateYolk y =Newyolk (); PublicEgg () {System.out.println ("New Egg ()"); } Public voidinsertyolk (yolk yy) {y=yy; } Public voidg () {y.f (); }} Public classBigeggextendsegg{ Public classYolkextendsegg.yolk{ Publicyolk () {System.out.println ("Bigegg.yolk ()"); } Public voidf () {System.out.println ("Bigegg.yolk.f ()"); } } PublicBigegg () {Insertyolk (Newyolk ()); } Public Static voidMain (string[] args) {Egg e=NewBigegg (); e.g (); }}/*output Egg.yolk () New Egg () Egg.yolk () Bigegg.yolk () Bigegg.yolk.f ()*/
The key point is also not mentioned in the book I think is the Bigegg when the constructor started to run (in fact, only the bigegg of the constructor did not output any information), so I added in the Bigegg constructor output "new Bigegg ()" statement. The results are as follows:
I think this is the truth here:
When the Bigegg constructor is called, its constructor first invokes the constructor of its parent class egg (that is, initializes its own parent class first), initializing the parent class's data field first when initializing the parent class egg. The data field of its parent egg has only a reference y of the private egg.yolk type, and y is initialized here, the constructor Egg.yolk () is called, so the first sentence "Egg.yolk ()" is output.
After initializing the data field of the egg, the egg's constructor (output "new Egg ()") is run, and the Bigegg constructor starts running (output "new Bigegg ()") after its parent class finishes initializing, and then runs the method Insertyolk of the parent egg, The constructor that runs Bigegg.yolk in its parameters section, where it becomes the part I just started with, then outputs "egg.yolk ()" "Bigegg.yolk ()", Finally, although the bigegg.yolk upward transformation, but because of the dynamic binding relationship, when calling E.f (), the output is still the Bigegg.yolk F method, Output "Bigegg.yolk.f ()".
Summary: The main reason for the problem here is due to the lack of detail about the initialization part of the fifth chapter (Knowledge Point: The parent class initialization is done before the subclass constructor is not running, or it can be said that the initialization of the parent class is started when the subclass constructor is called and the constructor of the subclass starts after its initialization is complete).
Think in Java reading notes: Inner class overrides and their initialization