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
總結
- 上一篇: Spring @Configuratio
- 下一篇: Java学习笔记-网络编程