繼承與多態,繼承多態

來源:互聯網
上載者:User

繼承與多態,繼承多態

一、運行 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關鍵字後,結果如。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.