[Java] 繼承複寫之探討
先來探討 Return 值得複寫 先來撰寫一個父類別 TestP.java 如下 import java . io . InputStream ; public class TestP { public void setInputStream ( InputStream is ) { } } 最基本的複寫 import java . io . InputStream ; public class TestP { public void setInputStream ( InputStream is ) { } } 請看InputStream的樹狀結構 測試1 : 先用父類別 Object import java . io . InputStream ; public class TestS extends TestP { @ Override public void setInputStream ( Object is ) { super . setInputStream ( is ) ; } } 運行結果有錯誤 測試2 : 用子類別測是 import java . io . FileInputStream ; import java . io . InputStream ; public class TestS extends TestP { @ Override public void setInputStream ( FileInputStream is ) { super . setInputStream ( is ) ; } } 結果還是錯 結論 參數的複寫,需要型別完全一樣,才能算是複寫,不然都是多形