动手动脑4
?
1.TestInherits.java
public class TestInherits {public static void main(String args[]){Child c = new Child();}}輸出結果:
修改代碼:
輸出結果:
結論:通過 super 調用基類構造方法,必須是子類構造方法中的第一個語句。
?
2.?為什么子類的構造方法在運行之前,必須調用父類的構造方法?能不能反過來?為什么不能反過來?
構造方法用于對基類的初始化。當構造一個對象時,先調用構造函數對成員函數和成員變量進行初始化,。子類繼承了父類的成員函數和成員變量,若不進行調用,則不會對父類的初始化。
3.ExplorationJDKSource.java
public class ExplorationJDKSource {/*** @param args*/public static void main(String[] args) {System.out.println(new A());}}class A{}
在編譯源碼時,當遇到沒有父類的類時,編譯器會定義的默認的父類(一般為Object),public void println(Object x),這一方法內部調用了String類的valueOf方法。valueOf方法內部又調用Object.toString方法:
public String toString() {
return getClass().getName() +"@" +
Integer.toHexString(hashCode());
}
?
4.
public class Fruit {public String toString(){return "Fruit toString.";}public static void main(String args[]){Fruit f=new Fruit();System.out.println("f="+f);// System.out.println("f="+f.toString());} }輸出結果:
?
?
結論:在“+”運算中,當任何一個對象與一個String對象,連接時,會隱式地調用其toString()方法,默認情況下,此方法返回“類名 @ + hashCode”。為了返回有意義的信息,子類可以重寫toString()方法。
5.?方法覆蓋
?
public class parents { public void parents(){System.out.println("parent!"); } public void parent1(){System.out.println("parent1!"); } } public class child extends parents { public void child(){super.parents(); } public void parents(){System.out.println("child"); } public static void main(String[]args){child ch=new child();ch.parents();ch.child();ch.parent1(); } }?
輸出結果:
?
6.?TestInstanceof.Java
public class TestInstanceof {public static void main(String[] args) {//聲明hello時使用Object類,則hello的編譯類型是Object,Object是所有類的父類//但hello變量的實際類型是StringObject hello = "Hello";//String是Object類的子類,所以返回true。System.out.println("字符串是否是Object類的實例:" + (hello instanceof Object));//返回true。System.out.println("字符串是否是String類的實例:" + (hello instanceof String));//返回false。System.out.println("字符串是否是Math類的實例:" + (hello instanceof Math));//String實現了Comparable接口,所以返回true。System.out.println("字符串是否是Comparable接口的實例:" + (hello instanceof Comparable));String a = "Hello";//String類既不是Math類,也不是Math類的父類,所以下面代碼編譯無法通過//System.out.println("字符串是否是Math類的實例:" + (a instanceof Math));} }
?
輸出結果:
7.?類型轉換
?
?
class Mammal{} class Dog extends Mammal {} class Cat extends Mammal{}public class TestCast {public static void main(String args[]){Mammal m;Dog d=new Dog();Cat c=new Cat();m=d;//d=m;d=(Dog)m;//d=c;//c=(Cat)m;} }
結果:d=m; ?d=c;有錯誤,c=(Cat)m正確
結論:在繼承中,子類可以自動轉換成父類,但父類轉換成子類只有引用類型真正身份才會轉換成功,否則會失敗。
?8.
public class ParentChildTest {public static void main(String[] args) {Parent parent=new Parent();parent.printValue();Child child=new Child();child.printValue();parent=child;parent.printValue();parent.myValue++;parent.printValue();((Child)parent).myValue++;parent.printValue();} }
?
?
?
結論:由此可得Java的一些語法特性(多態):
當子類與父類擁有一樣的方法,并且讓一個父類變量引用一個子類對象時,到底調用哪個方法,由對象自己的“真實”類型所決定,這就是說:對象是子類型的,它就調用子類型的方法,是父類型的,它就調用父類型的方法。
如果子類與父類有相同的字段,則子類中的字段會代替或隱藏父類的字段,子類方法中訪問的是子類中的字段(而不是父類中的字段)。如果子類方法確實想訪問父類中被隱藏的同名字段,可以用super關鍵字來訪問它。 如果子類被當作父類使用,則通過子類訪問的字段是父類的。
因此,我們進行程序設計時應避免子類與父類同名的字段!
9.
public class TestPolymorphism {public static void main(String args[]){Parent p=new Parent();p.Introduce();System.out.println(p.value);p=new Son();p.Introduce();System.out.println(p.value);p=new Daughter();p.Introduce();System.out.println(p.value);}
多態代碼:當多個類實現同一接口(或派生自同一抽象類)時,針對這些類所創建的對象調用接口所定義的方法時,會分別調用相應的類的具體實現代碼。
轉載于:https://www.cnblogs.com/NCLONG/p/9903877.html
總結
- 上一篇: 9.21渲染错误信息 参数化配置
- 下一篇: c++入门之运算符重载