java课程课后作业04之动手动脑
1.繼承條件下的構(gòu)造方法調(diào)用
先貼出實(shí)驗(yàn)需要的代碼:
1 package TestJava; 2 3 class Grandparent 4 { 5 public Grandparent() 6 { 7 8 System.out.println("GrandParent Created."); 9 10 } 11 12 13 public Grandparent(String string) 14 { 15 16 System.out.println("GrandParent Created.String:" + string); 17 18 } 19 20 } 21 22 23 24 class Parent extends Grandparent 25 { 26 27 28 public Parent() 29 { 30 31 //super("Hello.Grandparent."); 32 33 System.out.println("Parent Created"); 34 35 // super("Hello.Grandparent."); 36 37 } 38 39 } 40 41 42 43 class Child extends Parent 44 { 45 46 47 public Child() 48 { 49 50 System.out.println("Child Created"); 51 52 } 53 54 } 55 56 57 58 public class TestInherits 59 { 60 61 62 public static void main(String args[]) 63 { 64 65 Child c = new Child(); 66 67 } 68 69 }我們在第一次運(yùn)行的時(shí)候發(fā)現(xiàn)輸出順序是:
GrandParent Created
Parent Created
Child Created
結(jié)論1:當(dāng)我們?nèi)ミ\(yùn)行代碼的時(shí)候,父類的構(gòu)造函數(shù)會(huì)先于子類的構(gòu)造函數(shù)。
然后,在當(dāng)我們把子類中的父類的構(gòu)造方法取消注釋,我們會(huì)發(fā)現(xiàn)同樣的結(jié)果,然后當(dāng)我們把父類的構(gòu)造方法放入子類的構(gòu)造方法之后,我們會(huì)發(fā)現(xiàn),代碼無法編譯通過,由此得到結(jié)論。
結(jié)論2:通過 super 調(diào)用基類構(gòu)造方法,必須是子類構(gòu)造方法中的第一個(gè)語句。
結(jié)論2的原因:由于父類的構(gòu)造方法放在了子類的后面,如果一旦可以運(yùn)行,則違反了結(jié)論1,所以編譯無法通過。
2.判斷對象是否可以轉(zhuǎn)換
貼出實(shí)驗(yàn)代碼:
1 class Mammal{} 2 class Dog extends Mammal {} 3 class Cat extends Mammal{} 4 5 public class TestCast 6 { 7 public static void main(String args[]) 8 { 9 Mammal m; 10 Dog d=new Dog(); 11 Cat c=new Cat(); 12 m=d; 13 //d=m; 14 d=(Dog)m; 15 //d=c; 16 //c=(Cat)m; 17 18 } 19 }在沒有注釋的代碼編譯可以通過,當(dāng)中可以看出父類可以直接對子類進(jìn)行轉(zhuǎn)換,而子類如果要對父類進(jìn)行轉(zhuǎn)換的話必須要在父類的前面加上子類的名字來進(jìn)行強(qiáng)制轉(zhuǎn)換,否則編譯無法通過。
3.對于被重寫的父類如何被調(diào)用
貼出實(shí)驗(yàn)代碼:
1 package TestJava1; 2 public class ParentChildTest { 3 public static void main(String[] args) { 4 Parent parent=new Parent(); 5 parent.printValue();//100 6 Child child=new Child(); 7 child.printValue();//200 8 9 parent=child; 10 parent.printValue();//200 11 12 parent.myValue++; 13 parent.printValue();//200 14 15 ((Child)parent).myValue++; 16 parent.printValue();//201 17 18 } 19 } 20 21 class Parent{ 22 public int myValue=100; 23 public void printValue() { 24 System.out.println("Parent.printValue(),myValue="+myValue); 25 } 26 } 27 class Child extends Parent{ 28 public int myValue=200; 29 public void printValue() { 30 System.out.println("Child.printValue(),myValue="+myValue); 31 } 32 }運(yùn)行結(jié)果已經(jīng)寫在了注釋的后面。
分析:parent.myValue++;此行代碼的結(jié)果是對子類的myValue進(jìn)行編輯,而parent.printValue();卻是對父類的myValue數(shù)值進(jìn)行輸出,代碼((Child)parent).myValue++;是對父類的的數(shù)值編輯,所以再次輸出的時(shí)候就是201.
結(jié)論1:當(dāng)子類與父類擁有一樣的方法,并且讓一個(gè)父類變量引用一個(gè)子類對象時(shí),到底調(diào)用哪個(gè)方法,由對象自己的“真實(shí)”類型所決定,這就是說:對象是子類型的,它就調(diào)用子類型的方法,是父類型的,它就調(diào)用父類型的方法。
結(jié)論2:如果子類與父類有相同的字段,則子類中的字段會(huì)代替或隱藏父類的字段,子類方法中訪問的是子類中的字段(而不是父類中的字段)。如果子類方法確實(shí)想訪問父類中被隱藏的同名字段,可以用super關(guān)鍵字來訪問它。
結(jié)論3:如果子類被當(dāng)作父類使用,則通過子類訪問的字段是父類的!? ? ? ? ? ? ? ? ?就是這段代碼->?((Child)parent).myValue++;
轉(zhuǎn)載于:https://www.cnblogs.com/heiyang/p/9890601.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的java课程课后作业04之动手动脑的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 织梦教程
- 下一篇: JZOJ5944信标