11-5動手動腦
阿新 • • 發佈:2018-11-05
testInherits.java
Address.java
public final class Address { private final String detail; private final String postCode; //在構造方法裡初始化兩個例項屬性 public Address() { this.detail = ""; this.postCode = ""; } public Address(String detail , String postCode) {this.detail = detail; this.postCode = postCode; } //僅為兩個例項屬性提供getter方法 public String getDetail() { return this.detail; } public String getPostCode() { return this.postCode; } //重寫equals方法,判斷兩個物件是否相等。 public boolean equals(Object obj) {if (obj instanceof Address) { Address ad = (Address)obj; if (this.getDetail().equals(ad.getDetail()) && this.getPostCode().equals(ad.getPostCode())) { return true; } } return false; } public int hashCode() {return detail.hashCode() + postCode.hashCode(); } }
ExplorationJDKSource.java
public class ExplorationJDKSource { /** * @param args */ public static void main(String[] args) { System.out.println(new A()); } } class A{}
結果分析
main方法實際上呼叫的是:
public void println(Object x),這一方法內部呼叫了String類的valueOf方法。
valueOf方法內部又呼叫Object.toString方法:
public String toString() {
return getClass().getName() +"@" +
Integer.toHexString(hashCode());
}
hashCode方法是本地方法,由JVM設計者實現:
public native int hashCode()
Fruit.java
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()); } }
TestInstanceof.java。
TestInstanceof.java public class TestInstanceof { public static void main(String[] args) { //宣告hello時使用Object類,則hello的編譯型別是Object,Object是所有類的父類 //但hello變數的實際型別是String Object 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)); } }
TestCast.java
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; } }
第二個和第四個會引起編譯出錯
ParentChildTest.java
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(); } } class Parent{ public int myValue=100; public void printValue() { System.out.println("Parent.printValue(),myValue="+myValue); } } class Child extends Parent{ public int myValue=200; public void printValue() { System.out.println("Child.printValue(),myValue="+myValue); } }
TestPolymorphism.java
class Parent { public int value=100; public void Introduce() { System.out.println("I'm father"); } } class Son extends Parent { public int value=101; public void Introduce() { System.out.println("I'm son"); } } class Daughter extends Parent { public int value=102; public void Introduce() { System.out.println("I'm daughter"); } } 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); } }