JAVA中子類對象的實例化過程
Person.java
class Person

{
public String name = "not know";//4
public int age = -1 ;//5
public Person()

{
}
public Person(String name , int age)//3

{
this.name = name ;//6
this.age = age;//7
}
public void showInfo()

{
System.out.println("Name is :"+name+" Age is :"+age);
}
}
class Student extends Person

{
public String school = "not konwn";//8
public Student()

{
super();
}
public Student (String name ,int age, String school) //1

{
super(name,age); //2
this.school = school;//9
}
public void showInfo()

{
super.showInfo();
System.out.println("School is :"+school);
}
}
AllTest.java
class AllTest

{
public static void main(String[]args)

{
Student stA = new Student("wanghao",20,"qinghuadaxue"); //0
}
}
其語句的執行順序是怎樣的呢?
java中,在使用new操作符創建一個類的實例對象的時候,開始分配空間并將成員變量初始化為默認的數值,注意這里并不是指將變量初始化為在變量定義處的初始值,而是給整形賦值0,給字符串賦值null 這一點于C++不同,(student.name = null , student.age = 0 )
然后在進入類的構造函數。
在構造函數里面,首先要檢查是否有this或者super調用,this調用是完成本類本身的構造函數之間的調用,super調用是完成對父類的調用。二者只能出現一個,并且只能作為構造函數的第一句出現。在調用this和super的時候實現程序的跳轉,轉而執行被調用的this構造函數或者super構造函數。
在this和super執行完畢,程序轉而執行在類定義的時候進行的變量初始化工作。
這個執行完畢,才是構造函數中剩下的代碼的執行。
執行順序已經用綠色標出。
Order of initialization
Within a class, the order of initialization is determined by the order that the variables are defined within the class. The variable definitions may be scattered throughout and in between method definitions, but the variables are initialized before any methods can be called—even the constructor. For example: Feedback
//: c04:OrderOfInitialization.java
// Demonstrates initialization order.
import com.bruceeckel.simpletest.*;
// When the constructor is called to create a
// Tag object, you'll see a message:
class Tag {
Tag(int marker) {
System.out.println("Tag(" + marker + ")");
}
}
class Card {
Tag t1 = new Tag(1); // Before constructor
Card() {
// Indicate we're in the constructor:
System.out.println("Card()");
t3 = new Tag(33); // Reinitialize t3
}
Tag t2 = new Tag(2); // After constructor
void f() {
System.out.println("f()");
}
Tag t3 = new Tag(3); // At end
}
public class OrderOfInitialization {
static Test monitor = new Test();
public static void main(String[] args) {
Card t = new Card();
t.f(); // Shows that construction is done
monitor.expect(new String[] {
"Tag(1)",
"Tag(2)",
"Tag(3)",
"Card()",
"Tag(33)",
"f()"
});
}
} ///:~
//: c04:StaticInitialization.java
// Specifying initial values in a class definition.
class Bowl {
Bowl(int marker) {
System.out.println("Bowl(" + marker + ")");
}
void f(int marker) {
System.out.println("f(" + marker + ")");
}
}
class Table {
static Bowl b1 = new Bowl(1);
Table() {
System.out.println("Table()");
b2.f(1);
}
void f2(int marker) {
System.out.println("f2(" + marker + ")");
}
static Bowl b2 = new Bowl(2);
}
class Cupboard {
Bowl b3 = new Bowl(3);
static Bowl b4 = new Bowl(4);
Cupboard() {
System.out.println("Cupboard()");
b4.f(2);
}
void f3(int marker) {
System.out.println("f3(" + marker + ")");
}
static Bowl b5 = new Bowl(5);
}
public class StaticInitialization {
static Test monitor = new Test();
public static void main(String[] args) {
System.out.println("Creating new Cupboard() in main");
new Cupboard();
System.out.println("Creating new Cupboard() in main");
new Cupboard();
t2.f2(1);
t3.f3(1);
monitor.expect(new String[] {
"Bowl(1)",
"Bowl(2)",
"Table()",
"f(1)",
"Bowl(4)",
"Bowl(5)",
"Bowl(3)",
"Cupboard()",
"f(2)",
"Creating new Cupboard() in main",
"Bowl(3)",
"Cupboard()",
"f(2)",
"Creating new Cupboard() in main",
"Bowl(3)",
"Cupboard()",
"f(2)",
"f2(1)",
"f3(1)"
});
}
static Table t2 = new Table();
static Cupboard t3 = new Cupboard();
} ///:~
繼承情況下的初始化
了解一下包括繼承在內的初始化的過程將是非常有益的,這樣就能有個總
體的了解。看看下面這段代碼:
//: c06:Beetle.java
// The full process of initialization.
class Insect {
protected static Test monitor = new Test();
private int i = 9;
protected int j;
Insect() {
System.out.println("i = " + i + ", j = " + j);
j = 39;
}
private static int x1 = print("static Insect.x1 initialized");
static int print(String s) {
System.out.println(s);
return 47;
}
}
public class Beetle extends Insect {
private int k = print("Beetle.k initialized");
public Beetle() {
System.out.println("k = " + k);
System.out.println("j = " + j);
}
private static int x2 = print("static Beetle.x2 initialized");
public static void main(String[] args) {
System.out.println("Beetle constructor");
Beetle b = new Beetle();
monitor.expect(new String[] {
"static Insect.x1 initialized",
"static Beetle.x2 initialized",
"Beetle constructor",
"i = 9, j = 0",
"Beetle.k initialized",
"k = 47",
"j = 39"
});
}
} ///:~
當你用Java 運行Beetle 的時候,第一件事就是訪問了Beetel.main( )(這是一個static 方法),于是裝載器(loader)就會為
你尋找經編譯的Beetle 類的代碼(也就是Beetle.class 文件)。在裝載的過程中,裝載器注意到它有一個基類(也就是extends 所要表示的意思),于是它再裝載基類。不管你創不創建基類對象,這個過程總會發生。(試試看,把創建對象的那句注釋掉,看看會有什么結果。)如果基類還有基類,那么這第二個基類也會被裝載,以此類推。下一步,它會執行“根基類(root bas e class)”(這里就是Insect)的static 初始化,然后是下一個派生類的static 初始化,以此類推。這個順序非常重要,因為派生類的“靜態初始化(即前面講的static 初始化)”有可能要依賴基類成員的正確初始化。
現在所有必要的類都已經裝載結束,可以創建對象了。首先,對象里的所有的primitive 都會被設成它們的缺省值,而reference 也會被設成null——這個過程是一瞬間完成的,對象的內存會被統一地設置成“兩進制的零(binary zero)”。如果有對成員變量賦初值,則對成員變量進行賦值,然后調用基類的構造函數。調用是自動發生的,但是你可以使用super 來指定調用哪個構造函數(也就Beetle( )構造函數所做的第一件事)。基類的構造過程以及構造順序,同派生類的相同。基類構造函數運行完畢之后,會按照各個變量的字面順序進行初始化。最后會執行構造函數的其余部分。
posted on 2009-05-11 21:16
Frank_Fang 閱讀(2413)
評論(1) 編輯 收藏 所屬分類:
Java編程 、
C++編程