以下這個(gè)例子非常好的闡述了父子類同時(shí)存在時(shí)靜態(tài)代碼塊/靜態(tài)變量初始化,普通代碼塊/普通成員變量初始化,構(gòu)造器之間的具體運(yùn)行順序。
創(chuàng)新互聯(lián)是專業(yè)的玉州網(wǎng)站建設(shè)公司,玉州接單;提供網(wǎng)站設(shè)計(jì)制作、網(wǎng)站設(shè)計(jì),網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行玉州網(wǎng)站開發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!
注意,在構(gòu)造器開頭,我們可以看作按順序隱藏了以下兩行關(guān)鍵執(zhí)行步驟:
super();
普通代碼塊和普通屬性的初始化...
具體例子如下,運(yùn)行順序在代碼注釋中以數(shù)字括號(hào)給出~
public class CodeBlockDetail {
public static void main(String[] args) {
//(1) 進(jìn)行類的加載
//1.1 先加載 父類 A02 1.2 再加載 B02
//(2) 創(chuàng)建對(duì)象
//2.1 從子類的構(gòu)造器開始
new B02();//對(duì)象
}
}
class A02 { //父類
private static int n1 = getVal01();
static {
System.out.println("A02的一個(gè)靜態(tài)代碼塊..");//(2)
}
{
System.out.println("A02的第一個(gè)普通代碼塊..");//(5)
}
public int n3 = getVal02();//普通屬性的初始化
public static int getVal01() {
System.out.println("getVal01");//(1)
return 10;
}
public int getVal02() {
System.out.println("getVal02");//(6)
return 10;
}
public A02() {//構(gòu)造器
//隱藏
//super()
//普通代碼和普通屬性的初始化......
System.out.println("A02的構(gòu)造器");//(7)
}
}
class B02 extends A02 {
private static int n3 = getVal03();
static {
System.out.println("B02的一個(gè)靜態(tài)代碼塊..");//(4)
}
public int n5 = getVal04();
{
System.out.println("B02的第一個(gè)普通代碼塊..");//(9)
}
public static int getVal03() {
System.out.println("getVal03");//(3)
return 10;
}
public int getVal04() {
System.out.println("getVal04");//(8)
return 10;
}
public B02() {//構(gòu)造器
//隱藏了
//super()
//普通代碼塊和普通屬性的初始化...
System.out.println("B02的構(gòu)造器");//(10)
}
}
網(wǎng)頁(yè)名稱:Java代碼塊運(yùn)行順序細(xì)節(jié)闡述
網(wǎng)站路徑:http://chinadenli.net/article24/dsoioce.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、關(guān)鍵詞優(yōu)化、品牌網(wǎng)站設(shè)計(jì)、服務(wù)器托管、營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)