Before encountering such a problem in the project:
Class B inherits the class, a, and when it is instantiated, a method of B is called in the constructor of a, and the member property of B is initialized in the method of B, and then the member property of the B object is empty.
The code scenario is as follows:
When B New comes out, S will be "s = null" overrides the value of the call to Init in a, and if the method called in A is overridden by a quilt class, the A-to-image method is a child-object method, but a pair of like-to-call methods are the methods of the sub-objects, and then the code block of B is
In order to S=null overwrite the original initialization value, we can remove the initialization of s in the code block.
Let's take a detailed analysis of the code execution process during the construction of the subclass.
Legend:
This is a parent-child class model with static code blocks, initialization code blocks, constructor methods, and member methods, so the process of constructing TT2 () is as follows:
New TT2 ()->new TT1 (), static tt1 1-> static tt1 2-> tt1list = new ...-> tt1 constructor->tt2 static TT2 1-> tt2 static Tt2 2->list = null-> tt2 contructor->end
This may not be too clear, we use the graph to mark the following:
The output results are as follows:
Summary: Tired loading process no matter how complex, always follow-static code block---Common code block---construction method, the order of execution, in order to follow the step-by-step analysis, the structure of the program can be smoothed clear to avoid unnecessary errors.
PS: Internal class invocation of external class properties-internal classes when using external classes, when constructing an inner class, an external class is automatically added to the inner class, and the inner class is the method in the external class that is called through the reference.
Java Foundation-subclass inherits parent class instantiation object procedure