java 繼承的基礎(轉)

ba發表於2007-08-15
java 繼承的基礎(轉)[@more@]繼承是物件導向程式設計技術的一塊基石,因為它允許建立分等級層次的類。運用繼承,你能夠建立一個通用類,它定義了一系列相關專案的一般特性。該類可以被更具體的類繼承,每個具體的類都增加一些自己特有的東西。在Java 術語學中,被繼承的類叫超類(superclass ),繼承超類的類叫子類(subclass )。因此,子類是超類的一個專門用途的版本,它繼承了超類定義的所有例項變數和方法,並且為它自己增添了獨特的元素。
繼承一個類,只要用extends 關鍵字把一個類的定義合併到另一箇中就可以了。為了理解怎樣繼承,讓我們從簡短的程式開始。下面的例子建立了一個超類A和一個名為B的子類。注意怎樣用關鍵字extends 來建立A的一個子類。

// A simple example of inheritance.

// Create a superclass.
class A {
int i, j;

void showij() { System.out.println("i and j: " + i + " " + j); }}

// Create a subclass by extending class A.
class B extends A {
int k;

void showk() {

System.out.println("k: " + k); } void sum() {

System.out.println("i+j+k: " + (i+j+k));
}
}

class SimpleInheritance {

public static void main(String args[]) {
A superOb = new A();
B subOb = new B();

// The superclass may be used by itself. superOb.i = 10; superOb.j = 20;
System.out.println("Contents of superOb: "); superOb.showij(); System.out.println();

/* The subclass has access to all public members of

its superclass. */ subOb.i = 7; subOb.j = 8; subOb.k = 9; System.out.println("Contents of subOb: "); subOb.showij();subOb.showk(); System.out.println();

System.out.println("Sum of i, j and k in subOb:");
subOb.sum();
}
}

該程式的輸出如下:

Contents of superOb:
i and j: 10 20

Contents of subOb:
i and j: 7 8

k: 9

Sum of i, j and k in subOb:
i+j+k: 24

像你所看到的,子類B包括它的超類A中的所有成員。這是為什麼subOb 可以獲取i和j 以及呼叫showij( ) 方法的原因。同樣,sum( ) 內部,i和j可以被直接引用,就像它們是B的一部分。

儘管A是B的超類,它也是一個完全獨立的類。作為一個子類的超類並不意味著超類不能被自己使用。而且,一個子類可以是另一個類的超類。宣告一個繼承超類的類的通常形式如下:

class subclass-name extends superclass-name {
// body of class
}

你只能給你所建立的每個子類定義一個超類。Java 不支援多超類的繼承(這與C++ 不同,在C++中,你可以繼承多個基礎類)。你可以按照規定建立一個繼承的層次。該層次中,一個子類成為另一個子類的超類。然而,沒有類可以成為它自己的超類。

8.1.1 成員的訪問和繼承
儘管子類包括超類的所有成員,它不能訪問超類中被宣告成private 的成員。例如,考慮下面簡單的類層次結構:

/* In a class hierarchy, private members remain private to their class.

This program contains an error and will not
compile.
*/

// Create a superclass.

class A {int i; // public by default private int j; // private to A

void setij(int x, int y) { i = x; j = y;

}
}

// A's j is not accessible here.

class B extends A { int total; void sum() {

total = i + j; // ERROR, j is not accessible here
}
}

class Access {
public static void main(String args[]) {
B subOb = new B();

subOb.setij(10, 12);

subOb.sum();
System.out.println("Total is " + subOb.total);
}
}

該程式不會編譯,因為B中sum( ) 方法內部對j的引用是不合法的。既然j被宣告成private,它只能被它自己類中的其他成員訪問。子類沒權訪問它。

注意:一個被定義成private 的類成員為此類私有,它不能被該類外的所有程式碼訪問,包括子類。

8.1.2 更實際的例子
讓我們看一個更實際的例子,該例子有助於闡述繼承的作用。這裡,前面章節改進的Box類的最後版本將被擴充套件。它包括第四成員名為weight 。這樣,新的類將包含一個盒子的寬度、高度、深度和重量。

// This program uses inheritance to extend Box.

class Box { double width; double height; double depth;
// construct clone of an object

Box(Box ob) { // pass object to constructor width = ob.width; height = ob.height; depth = ob.depth;

}

// constructor used when all dimensions specified

Box(double w, double h, double d) { width = w; height = h; depth = d;

}

// constructor used when no dimensions specified

Box() { width = -1; // use -1 to indicate height = -1; // an uninitialized depth = -1; // box

}

// constructor used when cube is created Box(double len) { width = height = depth = len; }

// compute and return volume double volume() { return width * height * depth; }}

// Here, Box is extended to include weight.class BoxWeight extends Box {double weight; // weight of box

// constructor for BoxWeight

BoxWeight(double w, double h, double d, double m) { width = w; height = h; depth = d; weight = m;

}}class DemoBoxWeight {

public static void main(String args[]) { BoxWeight mybox1 = new BoxWeight(10, 20, 15, 34.3); BoxWeight mybox2 = new BoxWeight(2, 3, 4, 0.076); double vol;

vol = mybox1.volume(); System.out.println("Volume of mybox1 is " + vol); System.out.println("Weight of mybox1 is " + mybox1.weight); System.out.println();

vol = mybox2.volume(); System.out.println("Volume of mybox2 is " + vol); System.out.println("Weight of mybox2 is " + mybox2.weight);

}
}

該程式的輸出顯示如下:

Volume of mybox1 is 3000.0
Weight of mybox1 is 34.3

Volume of mybox2 is 24.0
Weight of mybox2 is 0.076

BoxWeight 繼承了Box 的所有特徵併為自己增添了一個weight 成員。沒有必要讓BoxWeight 重新建立Box 中的所有特徵。為滿足需要我們只要擴充套件Box就可以了。

繼承的一個主要優勢在於一旦你已經建立了一個超類,而該超類定義了適用於一組物件的屬性,它可用來建立任何數量的說明更多細節的子類。每一個子類能夠正好製作它自己的分類。例如,下面的類繼承了Box並增加了一個顏色屬性:

// Here, Box is extended to include color.
class ColorBox extends Box {
int color; // color of box

ColorBox(double w, double h, double d, int c) { width = w; height = h; depth = d; color = c;

}
}

記住,一旦你已經建立了一個定義了物件一般屬性的超類,該超類可以被繼承以生成特殊用途的類。每一個子類只增添它自己獨特的屬性。這是繼承的本質。

8.1.3 超類變數可以引用子類物件
超類的一個引用變數可以被任何從該超類派生的子類的引用賦值。你將發現繼承的這個方面在很多條件下是很有用的。例如,考慮下面的程式:

class RefDemo {

public static void main(String args[]) { BoxWeight weightbox = new BoxWeight(3, 5, 7, 8.37); Box plainbox = new Box(); double vol;

vol = weightbox.volume(); System.out.println("Volume of weightbox is " + vol); System.out.println("Weight of weightbox is " +

weightbox.weight); System.out.println();
// assign BoxWeight reference to Box reference

plainbox = weightbox;

vol = plainbox.volume(); // OK, volume() defined in Box

System.out.println("Volume of plainbox is " + vol);

/* The following statement is invalid because plainbox does not define a weight member. */ // System.out.println("Weight of plainbox is " + plainbox.weight); }}

這裡,weightbox 是BoxWeight 物件的一個引用,plainbox 是Box物件的一個引用。既然BoxWeight 是Box的一個子類,允許用一個weightbox 物件的引用給plainbox 賦值。

理解是引用變數的型別——而不是引用物件的型別——決定了什麼成員可以被訪問。也就是說,當一個子類物件的引用被賦給一個超類引用變數時,你只能訪問超類定義的物件的那一部分。這是為什麼plainbox 不能訪問weight 的原因,甚至是它引用了一個BoxWeight 物件也不行。仔細想一想,這是有道理的,因為超類不知道子類增加的屬性。這就是本程式中的最後一行被註釋掉的原因。Box的引用訪問weight 域是不可能的,因為它沒有定義。

儘管前面部分看起來有一點深奧,它是很重要的實際應用——本章後面將討論的兩種應用之一。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10617731/viewspace-957944/,如需轉載,請註明出處,否則將追究法律責任。

相關文章