1、重載是在一個類里一系列參數不同名字相同的方法.
方法簽名由方法名稱和一個參數列表(方法的參數的順序和類型)組成。只要簽名不同,就可以在一種類型內定義具有相同名稱的多種方法。當定義兩種或多種具有相同名稱的方法時,就稱作重載。
2、重寫是繼承后重新實現父類的方法.派生類型可以重寫繼承的虛方法
3、繼承是子類獲得父類的成員
4、多態則是父類使用子類的方法
下面給出一個例子:
一個父類:test_father,一個子類:test_sub,來說明這幾個概念的關系
test_father類:
public class test_father {
public test_father() {
System.out.println("father");//構造器
}
public void test_father() {
System.out.println("father");//重載
}
public int test_father(int i) {
System.out.println("fatherii"+i);
return i;//重載
}
}
test_sub類:
public class test_sub extends test_father{//繼承
public test_sub() {
System.out.println("test_sub");//構造器
}
public void test_father() {
System.out.println("sub");//重寫父類方法
}
public int test_father(int i) {
System.out.println("subii"+i);
return i;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
test_father fa=new test_sub();//多態
fa.test_father();
fa.test_father(3);
}
}
輸出結果:
father
test_sub
sub
subii3
posted on 2007-03-15 09:52
kelly 閱讀(665)
評論(0) 編輯 收藏 所屬分類:
java