一、運行 TestInherits.java 示例,觀察輸出,注意總結父類與子類之間構造方法的調用關系修改Parent構造方法的代碼,顯式調用GrandParent的另一個構造函數,注意這句調用代碼是否是第一句,影響重大!
class Grandparent {
public Grandparent() {
System.out.println("GrandParent Created.");
}
public Grandparent(String string) {
System.out.println("GrandParent Created.String:" + string);
}
}
class Parent extends Grandparent {
public Parent() {
//super("Hello.Grandparent.");
System.out.println("Parent Created");
// super("Hello.Grandparent.");
}
}
class Child extends Parent {
public Child() {
System.out.println("Child Created");
}
}
public class TestInherent {
public static void main(String args[]) {
Child c = new Child();
}
}
結果:

總結:通過 super 調用基類構造方法,必須是子類構造方法中的第一個語句。子類的構造方法在運行之前,必須調用父類的構造方法。因為繼承是在已有類的基礎上,添加新的變量與方法,從而產生一個新的類,子類是必須建立在父類的基礎上才能繼承,不能反過來。
二、不可變類的實例: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();
}
}
結果:無結果
總結:不可變的“類”有何用?(1)可以方便和安全地用於多線程環境中,(2)訪問它們可以不用加鎖,因而能提供較高的性能。
三、參看ExplorationJDKSource.java示例
此示例中定義了一個類A,它沒有任何成員:
class A { }
源程序:
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();
四、神奇的加號
源程序:
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());
}
}
結果:

總結:一個字串和一個對象“相加”,得到的結果為字符串,這是因為Fruit類覆蓋了Object類的toString方法。 在“+”運算中,當任何一個對象與一個String對象,連接時,會隱式地調用其toString()方法,默認情況下,此方法返回“類名 @ + hashCode”。為了返回有意義的信息,子類可以重寫toString()方法。方法覆蓋要求子類與父類的方法一模一樣,否則就是方法重載(overload)。
五、請自行編寫代碼測試以下特性(動手動腦):在子類中,若要調用父類中被覆蓋的方法,可以使用super關鍵字。
源程序:
class Grandparent{
public Grandparent(){//方法的重載
System.out.println("Grandparent Created.");
}
public Grandparent(String string){
System.out.println("GrandParent Created.String:"+string);
}
}
class Parent extends Grandparent{
public Parent(){
super("Hello.Grandparent");
System.out.println("Parent Created");
//super("Hello.Grandparent");
}
}
class Child extends Parent{
public Child(){
System.out.println("Child Created");
}
}
public class TestInherent {
public static void main(String args[]){
Child c=new Child();//構造方法
}
}
結果:

總結:在Parent子類中,若要調用父類Grandparent中被覆蓋的方法Grandparent(),使用super關鍵字後,結果如上圖。