以前面試或作題的時候老是會遇到問一個子類繼承一個父類後,裏面各自有靜態代碼塊,構造器,方法等的執行順序的問題,老是感受好像是什麼什麼樣,如今本身來寫個例子就知道了:
父類:面試
public class FatherDemo { public static int f1 = 3; private static String name = "father"; public FatherDemo(){ System.out.println("===f1:"+f1+"=====name:"+name+"========"); System.out.println("========我是Father類==無慘構造器============="); } public FatherDemo(String name){ System.out.println("===f1:"+f1+"=====name:"+name+"========"); System.out.println("========我是Father類==帶參構造器============="); this.name = name; System.out.println("===f1:"+f1+"=====name:"+name+"========"); } static{ System.out.println("===f1:"+f1+"=====name:"+name+"========"); System.out.println("========我是Father類==static代碼塊============="); } { System.out.println("===f1:"+f1+"=====name:"+name+"========"); System.out.println("========我是Father類==代碼塊============="); } public static void mothod0(){ System.out.println("===f1:"+f1+"=====name:"+name+"========"); System.out.println("========我是Father類==static方法mothod0============="); } public void method(){ System.out.println("===f1:"+f1+"=====name:"+name+"========"); System.out.println("========我是Father類==method方法============="); } }
子類:測試
public class ChildDemo extends FatherDemo{ public static int f2 = 3; private static String name = "child"; public ChildDemo(){ System.out.println("===f1:"+f1+"=====name:"+name+"========"); System.out.println("========我是ChildDemo類==無慘構造器============="); } public ChildDemo(String name){ System.out.println("===f1:"+f1+"=====name:"+name+"========"); System.out.println("========我是ChildDemo類==帶參構造器============="); this.name = name; System.out.println("===f1:"+f1+"=====name:"+name+"========"); } static{ System.out.println("===f1:"+f1+"=====name:"+name+"========"); System.out.println("========我是ChildDemo類==static代碼塊============="); } { System.out.println("===f1:"+f1+"=====name:"+name+"========"); System.out.println("========我是ChildDemo類==代碼塊============="); } public static void mothod0(){ System.out.println("===f1:"+f1+"=====name:"+name+"========"); System.out.println("========我是ChildDemo類==static方法mothod0============="); } public void method(){ System.out.println("===f1:"+f1+"=====name:"+name+"========"); System.out.println("===4444=====我是ChildDemo類==method方法============="); } }
測試類:this
public static void main(String[] args) { ChildDemo child = new ChildDemo(); child.method(); System.out.println(); ChildDemo demo = new ChildDemo("demo"); demo.method(); }
執行結果: ===f1:3=====name:father======== ========我是Father類==static代碼塊============= ===f1:3=====name:child======== ========我是ChildDemo類==static代碼塊============= ===f1:3=====name:father======== ========我是Father類==代碼塊============= ===f1:3=====name:father======== ========我是Father類==無慘構造器============= ===f1:3=====name:child======== ========我是ChildDemo類==代碼塊============= ===f1:3=====name:child======== ========我是ChildDemo類==無慘構造器============= ===f1:3=====name:child======== ===4444=====我是ChildDemo類==method方法=============code
===f1:3=====name:father======== ========我是Father類==代碼塊============= ===f1:3=====name:father======== ========我是Father類==無慘構造器============= ===f1:3=====name:child======== ========我是ChildDemo類==代碼塊============= ===f1:3=====name:demo======== ========我是ChildDemo類==帶參構造器============= ===f1:3=====name:demo======== ===f1:3=====name:demo======== ===4444=====我是ChildDemo類==method方法=============繼承
由此能夠看出,執行的順序爲:class
父類靜態屬性 --》父類靜態代碼塊--》子類靜態屬性--》子類靜態代碼塊--》父類代碼塊--》父類無參構造--》子類代碼塊--》子類無參構造--》子類方法方法
如上發現父類和子類的靜態方法都沒有執行,有意思的是當我實例化子類的帶參構造的時候,父類的子類的靜態代碼塊就不執行了,並且執行的順序變爲了: 父類代碼塊--》父類無參構造器--》子類代碼塊--》子類帶參構造器--》子類method方法demo