溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

java 繼承的基礎(chǔ)(轉(zhuǎn))

發(fā)布時(shí)間:2020-08-11 14:51:02 來(lái)源:ITPUB博客 閱讀:125 作者:ba 欄目:編程語(yǔ)言
java 繼承的基礎(chǔ)(轉(zhuǎn))[@more@]繼承是面向?qū)ο缶幊碳夹g(shù)的一塊基石,因?yàn)樗试S創(chuàng)建分等級(jí)層次的類。運(yùn)用繼承,你能夠創(chuàng)建一個(gè)通用類,它定義了一系列相關(guān)項(xiàng)目的一般特性。該類可以被更具體的類繼承,每個(gè)具體的類都增加一些自己特有的東西。在Java 術(shù)語(yǔ)學(xué)中,被繼承的類叫超類(superclass ),繼承超類的類叫子類(subclass )。因此,子類是超類的一個(gè)專門用途的版本,它繼承了超類定義的所有實(shí)例變量和方法,并且為它自己增添了獨(dú)特的元素。
繼承一個(gè)類,只要用extends 關(guān)鍵字把一個(gè)類的定義合并到另一個(gè)中就可以了。為了理解怎樣繼承,讓我們從簡(jiǎn)短的程序開始。下面的例子創(chuàng)建了一個(gè)超類A和一個(gè)名為B的子類。注意怎樣用關(guān)鍵字extends 來(lái)創(chuàng)建A的一個(gè)子類。

// 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 以及調(diào)用showij( ) 方法的原因。同樣,sum( ) 內(nèi)部,i和j可以被直接引用,就像它們是B的一部分。

盡管A是B的超類,它也是一個(gè)完全獨(dú)立的類。作為一個(gè)子類的超類并不意味著超類不能被自己使用。而且,一個(gè)子類可以是另一個(gè)類的超類。聲明一個(gè)繼承超類的類的通常形式如下:

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

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

8.1.1 成員的訪問(wèn)和繼承
盡管子類包括超類的所有成員,它不能訪問(wèn)超類中被聲明成private 的成員。例如,考慮下面簡(jiǎn)單的類層次結(jié)構(gòu):

/* 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);
}
}

該程序不會(huì)編譯,因?yàn)锽中sum( ) 方法內(nèi)部對(duì)j的引用是不合法的。既然j被聲明成private,它只能被它自己類中的其他成員訪問(wèn)。子類沒權(quán)訪問(wèn)它。

注意:一個(gè)被定義成private 的類成員為此類私有,它不能被該類外的所有代碼訪問(wèn),包括子類。

8.1.2 更實(shí)際的例子
讓我們看一個(gè)更實(shí)際的例子,該例子有助于闡述繼承的作用。這里,前面章節(jié)改進(jìn)的Box類的最后版本將被擴(kuò)展。它包括第四成員名為weight 。這樣,新的類將包含一個(gè)盒子的寬度、高度、深度和重量。

// 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 的所有特征并為自己增添了一個(gè)weight 成員。沒有必要讓BoxWeight 重新創(chuàng)建Box 中的所有特征。為滿足需要我們只要擴(kuò)展Box就可以了。

繼承的一個(gè)主要優(yōu)勢(shì)在于一旦你已經(jīng)創(chuàng)建了一個(gè)超類,而該超類定義了適用于一組對(duì)象的屬性,它可用來(lái)創(chuàng)建任何數(shù)量的說(shuō)明更多細(xì)節(jié)的子類。每一個(gè)子類能夠正好制作它自己的分類。例如,下面的類繼承了Box并增加了一個(gè)顏色屬性:

// 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;

}
}

記住,一旦你已經(jīng)創(chuàng)建了一個(gè)定義了對(duì)象一般屬性的超類,該超類可以被繼承以生成特殊用途的類。每一個(gè)子類只增添它自己獨(dú)特的屬性。這是繼承的本質(zhì)。

8.1.3 超類變量可以引用子類對(duì)象
超類的一個(gè)引用變量可以被任何從該超類派生的子類的引用賦值。你將發(fā)現(xiàn)繼承的這個(gè)方面在很多條件下是很有用的。例如,考慮下面的程序:

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 對(duì)象的一個(gè)引用,plainbox 是Box對(duì)象的一個(gè)引用。既然BoxWeight 是Box的一個(gè)子類,允許用一個(gè)weightbox 對(duì)象的引用給plainbox 賦值。

理解是引用變量的類型——而不是引用對(duì)象的類型——決定了什么成員可以被訪問(wèn)。也就是說(shuō),當(dāng)一個(gè)子類對(duì)象的引用被賦給一個(gè)超類引用變量時(shí),你只能訪問(wèn)超類定義的對(duì)象的那一部分。這是為什么plainbox 不能訪問(wèn)weight 的原因,甚至是它引用了一個(gè)BoxWeight 對(duì)象也不行。仔細(xì)想一想,這是有道理的,因?yàn)槌惒恢雷宇愒黾拥膶傩?。這就是本程序中的最后一行被注釋掉的原因。Box的引用訪問(wèn)weight 域是不可能的,因?yàn)樗鼪]有定義。

盡管前面部分看起來(lái)有一點(diǎn)深?yuàn)W,它是很重要的實(shí)際應(yīng)用——本章后面將討論的兩種應(yīng)用之一。
向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI