標題起得略拗口,大概意思就是說在一個Java類中,域和構造方法的調用順序。
1. 沒有繼承的情況
單獨一個類的場景下,初始化順序為依次為 靜態數據,繼承的基類的構造函數,成員變量,被調用的構造函數。
其中靜態數據只會初始化一次。
package com.khlin.binding.test; public class App2 { public static void main(String[] args) { Son son = new Son(); } } class Son { public Son() { System.out.println("this is son."); } public Son(int age) { System.out.println("son is " + age + " years old."); } private Height height = new Height(1.8f); public static Gender gender = new Gender(true); } class Height { public Height(float height) { System.out.println("initializing height " + height + " meters."); } } class Gender { public Gender(boolean isMale) { if (isMale) { System.out.println("this is a male."); } else { System.out.println("this is a female."); } } }
輸出:
2. 繼承的情況
稍微修改一下代碼,添加兩個基類,讓Son繼承Father, Father繼承Grandpa。
繼承的情況就比較復雜了。由於繼承了基類,還將往上回溯,遞歸地調用基類的無參構造方法。
在我們的例子中,在初始化靜態數據后,會先往上追溯,調用Father的默認構造方法,此時再往上追溯到Grandpa的默認構造方法。
注:如果在子類的構造方法中,顯式地調用了父類的帶參構造方法,那么JVM將調用指定的構造方法而非默認構造方法。
基類和子類均有靜態數據,成員變量和構造方法的場景
我們繼續修改代碼,讓其最終呈現如下:
1 package com.khlin.binding.test; 2 3 public class App2 { 4 public static void main(String[] args) { 5 Son son = new Son(); 6 } 7 } 8 9 class Grandpa { 10 public Grandpa() { 11 System.out.println("this is grandpa."); 12 } 13 14 public Grandpa(int age) { 15 System.out.println("grandpa is " + age + " years old."); 16 } 17 18 private Height height = new Height(1.5f); 19 20 public static Gender gender = new Gender(true, "grandpa"); 21 } 22 23 class Father extends Grandpa { 24 25 public Father() { 26 System.out.println("this is father."); 27 } 28 29 public Father(int age) { 30 System.out.println("father is " + age + " years old."); 31 } 32 33 private Height height = new Height(1.6f); 34 35 public static Gender gender = new Gender(true, "father"); 36 } 37 38 class Son extends Father { 39 40 public Son() { 41 super(50); 42 System.out.println("this is son."); 43 } 44 45 public Son(int age) { 46 System.out.println("son is " + age + " years old."); 47 } 48 49 private Height height = new Height(1.8f); 50 51 public static Gender gender = new Gender(true, "son"); 52 } 53 54 class Height { 55 public Height(float height) { 56 System.out.println("initializing height " + height + " meters."); 57 } 58 } 59 60 class Gender { 61 public Gender(boolean isMale) { 62 if (isMale) { 63 System.out.println("this is a male."); 64 } else { 65 System.out.println("this is a female."); 66 } 67 } 68 69 public Gender(boolean isMale, String identify) { 70 if (isMale) { 71 System.out.println(identify + " is a male."); 72 } else { 73 System.out.println(identify + " is a female."); 74 } 75 } 76 }
最后輸出會是什么呢?
參考下面另一個案例的分析。鏈接:http://bbs.csdn.net/topics/310164953
在我們的示例中,加載順序應該是這樣的:
Grandpa 靜態數據
Father 靜態數據
Son 靜態數據
Grandpa 成員變量
Grandpa 構造方法
Father 成員變量
Father 構造方法
Son 成員變量
Son 構造方法
所以輸出如下: