[Java] based learning call the constructor of the order of [pit]

Explain: super herein () is only used placeholder, in fact, must be strictly in accordance with the process of initializing the hierarchical:
1. initialize the parent class member variables X, i.e., initialization of member variables Y, print out: the Y
2 . constructor initializes the parent class X, printing out: X
3. after initialization is complete parent class, subclass Z initialization, initialize subclass member variables y, print out: the Y
4. then initialize subclass constructor Z, Print out: Z

As can be seen, super () where the order does not affect the initialization

 1 class X {
 2     Y b = new Y();
 3     X() {
 4         System.out.print("X");
 5     }
 6 }
 7  
 8 class Y {
 9  
10     Y() {
11         System.out.print("Y");
12     }
13 }
14  
15 public class Z extends X {
16  
17     Y y = new Y();
18  
19     Z() {
20         //super();
21         System.out.print("Z");
22     }
23  
24     public static void main(String[] args) {
25         new Z(); 
26     }
27 }

The results: YXYZ

Guess you like

Origin www.cnblogs.com/pengge666/p/12008067.html