日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java 继承初探

發布時間:2024/4/15 java 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java 继承初探 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Java繼承的基礎

Java中,被繼承的類叫做超類,繼承超類的類叫子類。(一個子類亦可以是另一個類的超類)

繼承一個類,只需要用關鍵字 extends 把一個類的定義合并到另一個類中就可以了。

例子中創建了一個超類A和一個名為B的子類。

class A {

???????? int i,j ;

???????? void showij() {

?????????????????? System.out.println( "i and j : " + i + " " + j ) ;

???????? }

}

?

class B extends A {?

???????? // B類繼承了A類 由此B類可以直接使用A類的所有內容

?

???????? int k ;

????????

???????? void showk() {

?????????????????? System.out.println("K: " + k ) ;

???????? }

???????? void sum() { // 因為B繼承了A所以可以獲得A類中的變量i,j

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

???????? }

}

?

public class SimpleInheritance {

???????? public static void main(String args[]) {

?????????????????? A superOb = new A() ;

?????????????????? B subOb = new B() ;

?

?????????????????? superOb.i = 10 ;

?????????????????? superOb.j = 20 ;

?????????????????? System.out.println("Contents of superOb: ") ;

?????????????????? superOb.showij() ;

?????????????????? System.out.println() ;

?

?????????????????? subOb.i = 7 ;

?????????????????? subOb.j = 8 ;

?????????????????? subOb.k = 9 ;

?????????????????? System.out.println("Contents of subOb: ") ;

?????????????????? subOb.showij() ;?

?????????????????? // 因為繼承A類,所以B類的實例對象可以調用A類的方法

?

?????????????????? subOb.showk() ;

?????????????????? System.out.println() ;

?

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

?????????????????? subOb.sum() ;

?

???????? }

}

?

雖然子類包括超類的所有成員,但是子類不能訪問超類中被聲明為private的成員。

?

class Box {

???????? double width ;

???????? double height ;

???????? double depth ;

?

???????? Box(Box ob) {

?????????????????? width = ob.width ;

?????????????????? height = ob.height ;

?????????????????? depth = ob.depth ;

???????? }

?

???????? Box() {

?????????????????? width = -1 ;

?????????????????? height = -1 ;

?????????????????? depth = -1 ;

???????? }

????????

???????? Box(double len) {

?????????????????? width = height = depth = len ;

???????? }

?

???????? double volume() {

?????????????????? return width * height * depth ;

???????? }

}

?

class BoxWeight extends Box {? // BoxWeight 繼承了Box的所有特征(功能)

??? // 在繼承Box類后,子類BoxWeight也可以在不改變Box類的情況下獨立完成成員的添加

?

??? double weight ;? //為自己添加了一個變量成員

?

??? BoxWeight (double w , double h , double d , double m ) {

?????? width = w ;

?????? height = h ;

?????? depth = d ;

?????? weight = m ;

??? }

}

?

public 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) ;

?????????????????? System.out.println();

???????? }

}

?

?

超類的一個引用變量可以被任何從該超類派生的子類的引用賦值。

理解是引用變量的類型,而不是引用對象的類型;決定了什么成員可以被訪問。

?

也就是說,當一個子類對象的引用被賦給一個超類引用變量時,你只能訪問超類定義的對象的那一部分。這就是下例中為什么plainbox不能范文weight的原因,甚至是他引用了一個BoxWeight對象也不行。

因為超類不知道子類增加的屬性(反之則知道)。

下例中,Box的引用訪問weight域是不可能的,因為Box類沒有定義。

?

?

class RefDemo {

???????? public static void main(String args[]) {

?????????????????? // weightbox 是 BoxWeight對象的一個引用,

?????????????????? BoxWeight weightbox = new BoxWeight(3,5,7,8.37) ;

?????????????????? // plainbox是Box對象的一個引用,

?????????????????? 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() ;

?

???????? ???????? plainbox = weightbox ; ?// weightbox的對象引用給plainbox賦值*

?

?????????????????? vol = plainbox.volume() ;

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

?????????????????? //System.out.println("Weight of plainbox is? " + plainbox.weight) ;

?????????????????? // 不可以訪問 weight,因為在超類中沒有賦予Box.plainbox訪問的權利

???????? }

}

轉載于:https://www.cnblogs.com/wangyuyang1016/p/10604771.html

總結

以上是生活随笔為你收集整理的Java 继承初探的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。