下面代碼運行的結果是什麼?ide
Father 類code
/** * @author kevin * @date 2019/7/8 15:48 */ public class Father { private int i = test(); private static int j = method(); static { System.out.print("(1)"); } Father(){ System.out.print("(2)"); } { System.out.print("(3)"); } public int test(){ System.out.print("(4)"); return 1; } private static int method() { System.out.print("(5)"); return 1; } }
Son 類對象
/** * @author kevin * @date 2019/7/8 15:55 */ public class Son extends Father { private int i = test(); private static int j = method(); static { System.out.print("(6)"); } Son(){ System.out.print("(7)"); } { System.out.print("(8)"); } public static int method(){ System.out.print("(10)"); return 1; } @Override public int test() { System.out.print("(9)"); return 1; } public static void main(String[] args) { Son s1 = new Son(); System.out.println(); Son s2 = new Son(); } }
<clinit>()
方法
<clinit>()
方法由靜態類變量顯示賦值代碼和靜態代碼塊組成<clinit>()
方法只執行一次1、先執行父類初始化get
<clinit>()
方法2、再執行子類初始化虛擬機
<clinit>()
方法整個類初始化完成後,輸出it
(5)(1) (10) (6)
<init>()
方法
<init>()
方法可能重載有多個,有幾個構造器就有幾個<init>()
方法<init>()
方法由非靜態實例變量顯示賦值代碼和非靜態代碼塊、對應的構造器組成<init>()
方法<init>()
方法首行是super()或super(參數),即父類的<init>()
方法io
test()
方法,會執行子類的test()
方法(9)(3)(2)(9)(8)(7)
因此整個結果爲class
(5)(1)(10)(6)(9)(3)(2)(9)(8)(7) (9)(3)(2)(9)(8)(7)
其實,這主要考驗你們對類初始化和實例化的考驗,以及父類子類之間的關係。
我在stackoverflow
找到一個回答,感受很好,簡潔的解釋了初始化和實例化test
<init>
is the (or one of the) constructor(s) for the instance, and non-static field initialization.變量
<clinit>
are the static initialization blocks for the class, and static field initialization.
class X { static Log log = LogFactory.getLog(); // <clinit> private int x = 1; // <init> X(){ // <init> } static { // <clinit> } }
固然這裏只是簡單分析了一下,若是你們想更深刻了解,你們能夠看看 《深刻JAVA虛擬機第二版.》這本書。 好了玩的開心!