java方法的重載、覆蓋和隱藏
重載 overloading
大家都熟悉java的重載以及實現,在一個類中同一個函數以多種不同的形態出現,即函數的參數個數或者類型不一樣。
例子 System.out.println(); 下面簡稱SOP
SOP(String str)
SOP(int number)
SOP(char ch)
SOP(double num)
.................................
這就是重載的效果
覆蓋(重寫) overwritting
覆蓋也是大家熟悉不過了的,子類繼承父類,然後子類中覆蓋原父類的方法,從而實例化子類後,調用的是子類的方法。
例子
public class father{
public overwritting(){
SOP("father method");
}
public class son extends father{
public overwritting(){
SOP("son method");
}
public static void main(String args[]){
father son=new son();
son.overwritting();
}
}
結果會調用子類的overwritting方法, son method。這就是覆蓋。
隱藏 hide
大家知道了覆蓋,那麼什麼情況下,子類實例化後會調用父類的方法而不是子類的方法呢?
(當然子類和父類的方法定義需要是一樣的)
大家知道static 類型的方法是不能被覆蓋的,所以java利用這一個特性完成了隱藏的效果。
例子
public class father{
public static overwritting(){
SOP("father method");
}
public class son extends father{
public static overwritting(){
SOP("son method");
}
public static void main(String args[]){
father son=new son();
son.overwritting();
}
}
結果會調用父類的overwritting方法 fathetr method。這就是隱藏。
━━━━━━━━━━━━━━━━━━━━━
方法的重寫、重載及隱藏
首先,重寫和隱藏是發生在兩個類中的,而重載可以發生在一個類中。
重寫的概念就是顧名思義了:重新寫一遍;方法名、參數及返回值是一模一樣的,可能實現的過程不一樣,爲什麼要重寫?因爲原來的方法不夠perfect 或者不夠strong,或者達不到開發者的實際應用要求。
重載是當多個方法享有相同的名字,但是這些方法的參數不同,或者是參數的個數不同,或者是參數類型不同時。就叫方法的重載 要注意的是:返回類型不能用來區分重載的方法,僅僅返回類型不同的兩個同名方法是個error,這點比較容易理解了:如果你寫參數一模一樣的但是返回類型不一樣的方法,當調用的時候,編譯器沒法決定調哪個好
隱藏 如下,是華爲的一道面試題
public class classA{ public void methodOne(int i){ } public void methodTwo(int i){ } public static void methodThree(int i){ } public static void methodFour(int i){ } } public class classB extends classA{ public static void methodOne(int i){ } public void methodTwo(int i){ } public void methodThree(int i){ } public static void methodFour(int i){ } }
1 問那些方法隱藏了父類的方法? 2 問那些方法覆蓋了父類的方法?
其實關於隱藏的概念本人不太瞭解,因爲基本沒用過,但是上題中的重寫是很明顯的,那就是methodTow,methodOne和methodThree就比較明顯的是個錯誤了,子類繼承父類,兩個相同的方法一個是static的,一個不是,你讓編譯器怎麼實例化啊,那剩下的就是隱藏了,根據網上搜到的概念:static 類型的方法是不能被覆蓋的;也就是說子類的methodFour寫了也沒用,還是調用父類的methodFour方法 Sample: public class father{ public static void overwritting(){ System.out.print("father method"); } }
public class son extends father{ public static void overwritting(){ System.out.print("son method"); } public static void main(String args[]){ father son=new son(); son.overwritting(); } } 以上程序的運行結果就是輸出了:father method
━━━━━━━━━━━━━━━━━━━━━
重載,繼承,重寫和多態的區別:
http://www.oioj.net/blog/user2/20387/archives/2005/117656.shtml
|
重載,繼承,重寫和多態的區別: 繼承是子類獲得父類的成員,重寫是繼承後重新實現父類的方法。重載是在一個類裏一系列參數不同名字相同的方法。多態則是爲了避免在父類裏大量重載引起代碼臃腫且難於維護。 網上看到一個有趣的說法是:繼承是子類使用父類的方法,而多態則是父類使用子類的方法。 下面的例子包含了這四種實現: class Triangle extends Shape { public int getSides() { return 3; } } class Rectangle extends Shape { public int getSides(int i) { return i; } } public class Shape { public boolean isSharp(){ return true; } public int getSides(){ return 0 ; } public int getSides(Triangle tri){ return 3 ; } public int getSides(Rectangle rec){ return 4 ; } public static void main(String[] args) { Triangle tri = new Triangle(); System.out.println("Triangle is a type of sharp? " + tri.isSharp()); Shape shape = new Triangle(); System.out.println("My shape has " + shape.getSides() + " sides."); } } 紅色是重載,綠色是重寫,藍色是繼承,粉紅是多態 注意Triangle類的方法是重寫,而Rectangle類的方法是重載。 比較紅色的和粉紅的部分就可以發現多態對重載的優點:如果用重載,則在父類裏要對應每一個子類都重載一個取得邊數的方法;如果用多態,則父類只提供取得邊數的接口,至於取得哪個形狀的邊數,怎樣取得,在子類裏各自實現(重寫)。 |
|