When an object is created, the object initializes the procedure:
1, the implicit initialization of the data members in the class. (Num is implicitly initialized to 0 in the following figure example)
2, the subclass of the constructor into the stack.
3, the parent class constructor is initialized. (Because the first row of the subclass constructor has an implicit super ())
4, the subclass of the member variable display initialization. (Num is initialized to 9 in the following figure example)
5, the construction of code block display initialization.
Class Fu
{
fu ()
{
System.out.println ("Fu constructor Run ...");
Show ();
}
void Show ()
{
System.out.println ("Fu show Run");
}
Class Zi extends Fu
{
int num = 9;
{
System.out.println ("constructor code ..." + num);
num = ten;
}
Zi ()
{
System.out.println ("Zi constructor run ..." + num);
}
void Show ()
{
System.out.println ("Zi show run ..." + num);
}
public class Classtest
{public
static void Main (string[] args)
{
new Zi ();
}
}
/* Run output
Fu constructor run ...
Zi Show Run ... 0
Constructor Code ... 9
Zi Constructor run ... Ten
* *
The parent class constructor calls show () in a subclass because the child class object is currently established, and this pointer points to the current subclass object.
The show () method of the parent class object is overwritten by the show () method of the Quilt class object, so now find the show () method in the Subclass object.
If not, find the show () method in the parent class object.