Java 继承初探
Java繼承的基礎(chǔ)
Java中,被繼承的類叫做超類,繼承超類的類叫子類。(一個(gè)子類亦可以是另一個(gè)類的超類)
繼承一個(gè)類,只需要用關(guān)鍵字 extends 把一個(gè)類的定義合并到另一個(gè)類中就可以了。
例子中創(chuàng)建了一個(gè)超類A和一個(gè)名為B的子類。
| class A { ???????? int i,j ; ???????? void showij() { ?????????????????? System.out.println( "i and j : " + i + " " + j ) ; ???????? } } ? class B extends A {? ???????? // B類繼承了A類 由此B類可以直接使用A類的所有內(nèi)容 ? ???????? int k ; ???????? ???????? void showk() { ?????????????????? System.out.println("K: " + k ) ; ???????? } ???????? void sum() { // 因?yàn)锽繼承了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() ;? ?????????????????? // 因?yàn)槔^承A類,所以B類的實(shí)例對(duì)象可以調(diào)用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類的情況下獨(dú)立完成成員的添加 ? ??? double weight ;? //為自己添加了一個(gè)變量成員 ? ??? 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(); ???????? } } |
?
?
超類的一個(gè)引用變量可以被任何從該超類派生的子類的引用賦值。
理解是引用變量的類型,而不是引用對(duì)象的類型;決定了什么成員可以被訪問。
?
也就是說,當(dāng)一個(gè)子類對(duì)象的引用被賦給一個(gè)超類引用變量時(shí),你只能訪問超類定義的對(duì)象的那一部分。這就是下例中為什么plainbox不能范文weight的原因,甚至是他引用了一個(gè)BoxWeight對(duì)象也不行。
因?yàn)槌惒恢雷宇愒黾拥膶傩?#xff08;反之則知道)。
下例中,Box的引用訪問weight域是不可能的,因?yàn)锽ox類沒有定義。
?
?
| class RefDemo { ???????? public static void main(String args[]) { ?????????????????? // weightbox 是 BoxWeight對(duì)象的一個(gè)引用, ?????????????????? BoxWeight weightbox = new BoxWeight(3,5,7,8.37) ; ?????????????????? // plainbox是Box對(duì)象的一個(gè)引用, ?????????????????? 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的對(duì)象引用給plainbox賦值* ? ?????????????????? vol = plainbox.volume() ; ?????????????????? System.out.println("Volume of plainbox is " + vol) ; ?????????????????? //System.out.println("Weight of plainbox is? " + plainbox.weight) ; ?????????????????? // 不可以訪問 weight,因?yàn)樵诔愔袥]有賦予Box.plainbox訪問的權(quán)利 ???????? } } |
轉(zhuǎn)載于:https://www.cnblogs.com/wangyuyang1016/p/10604771.html
總結(jié)
- 上一篇: Spring @Configuratio
- 下一篇: Codeforces刷题