java經典回顧

來源:互聯網
上載者:User
Code:
//Insect.class
package test;

class Insect {
int i = 9;
int h=y1;
static int y1=8;
int j;

Insect() {
prt("i = " + i + ", j = " + j);
j = 39;
}

static int x1 = prt("static Insect.x1 initialized");

static int prt(String s) {
System.out.println(s);
return 47;
}
}

//Beetle.class
public class Beetle extends Insect {
int k = prt("Beetle.k initialized");

Beetle() {
prt("k = " + k);
prt("j = " + j);
}

static int x2 = prt("static Beetle.x2 initialized");

public static void main(String[] args) {
prt("Beetle constructor");
Beetle b = new Beetle();

}
}
/* 1。首先試著訪問Beetle.main() 這是一個靜態方法。
* 2。到外面去找到為Beetle類編譯好的代碼Beetle.class
* 3。裝載Beetle.class過程中發現Beetle有基類。
* 4。初始化基類
* 1)先初始化static
* 2)然後按文字順序繼續初始化普通變數
* 5。初始化衍生類別
* 1)先初始化static
* 2)然後按文字順序繼續初始化普通變數
* 6。執行main裡的主體程式(按文字順序執行)
* 1)prt("Beetle constructor");
* 2)執行個體化一個衍生類別(Beetle b = new Beetle();)
* 1)基類建構函式
* 2)衍生類別建構函式
*
* 考點:static變數的初始化和訪問;建構函式的使用
* */

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.