java父類與子類的多態性

來源:互聯網
上載者:User
父類:public class base {public String s1 = "base string";public static String s2 = "base static string";public void show(){System.out.println(this.s1);}public void show_static(){System.out.println(s2);}}

子類:

public class child extends base {public String s1 = "child string";public static String s2 = "child static string";public void show(){System.out.println(this.s1);}public void show_static(){System.out.println(s2);}}

調用

public class mytest {public static void main(String[] args){/*base b1 = new base();b1.show();child c1 = new child();c1.show(); */base t1 = new child();t1.show();t1.show_static();System.out.println(t1.s1);System.out.println(t1.s2);}}

結果:

child stringchild static stringbase stringbase static string

結論:

多態支援執行個體方法,對於執行個體的欄位,執行個體的靜態方法等都是不支援多態。

即:執行個體方法的引用找的是與執行個體對應;而靜態內容引用找的是與變數對應。

----------------------------------------------------------父類變數調用子類執行個體的分割線-----------------------------------------------------------------------

通常會在代碼裡看到用父類的變數來接收子類的執行個體,然後再調用相應的方法,這樣做沒有什麼特殊的用法,其好處減低代碼的耦合度,使代碼易於維護。

先看一個例子,

public class base {public String s1 = "base string";public static String s2 = "base static string";public void show(){System.out.println(this.s1);}public void show_static(){System.out.println(s2);}}public class child extends base {public String s1 = "child string";public static String s2 = "child static string";public void show(){System.out.println(this.s1);}public void show_static(){System.out.println(s2);}}public class child2 extends base {public String s1 = "child2 string";public static String s2 = "child2 static string";public void show(){System.out.println(this.s1);}public void show_static(){System.out.println(s2);}}

調用類:

public class mytest {public void f1(child cc){cc.show();}public void f2(base bb){bb.show();}public static void main(String[] args){mytest m1 = new mytest();child c1 = new child();m1.f1(c1);base b1 = new child();m1.f2(b1);}}

上面是有2個方法f1,f2開始幹了同樣的事情;之後有需求要改變,需要把child類換類child2類;那麼對於f1一套的東西來說要改3個地方【申明執行個體2處和參數申明1處】,而對於f2一套的東西就只改一處地方【執行個體處】。

想想如果參數申明的地方不是一個而是多個,甚至不同類,不同包裡,改的地方就多了,還容易漏。所以f1的一套流程就沒有f2的一套流程好維護。

修改為child2後的情況見下:

public class mytest {public void f1(child2 cc){cc.show();}public void f2(base bb){bb.show();}public static void main(String[] args){mytest m1 = new mytest();child2 c1 = new child2();m1.f1(c1);base b1 = new child2();m1.f2(b1);}}

----------------------------------------------------------擴充說明的分割線-----------------------------------------------------------------------

其實嚴格來說上面的應用還不夠嚴謹,因為雖然父類是同樣的,但子類繼承父類後自己可以任意擴充的;即有可能child和child2所包含的方法不是完全一致的,這樣直接等價替換,f2在調用時就有可能出現找不到方法的異常。所以最好的情況是child和child2繼承自同一個介面而不是同一個類,這樣就可以避免調用錯誤的問題了,你想怎麼換就怎麼換,不用再考慮2個替換類之間的內容差異了,只要新的類能完成新需求即可。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.