方法重寫是子類繼承父類(默認繼承Object類)后覆蓋父類的方法 需要保證同名 同參 同返回值 且訪問權限范圍不能縮小(public>protected>default>private)文章來源:http://www.zghlxwxcb.cn/news/detail-419477.html
public class Father{
public int method(){
return -1;
}
}
class Son extends Father{
//訪問范圍不能小 返回值 方法名 參數(shù) 全部一致!
//方法重寫/方法覆蓋
public int method(){
return -2;
}
}
方法重載是同類中可以有多個同名但參數(shù)不同的方法 調用方法的時候可根據傳參情況執(zhí)行對應的方法 需要注意參數(shù)不同可以指參數(shù)數(shù)量不同 也可以指數(shù)量相同但數(shù)據類型不同文章來源地址http://www.zghlxwxcb.cn/news/detail-419477.html
public class Test{
public int sum(int a,int b){
return a+b;
}
//參數(shù)類型不同
public int sum(double a,double b){
return a+b;
}
//參數(shù)個數(shù)不同
public int sum(int a,int b,int c){
return a+b+c;
}
}
到了這里,關于關于Java中方法重載和方法重寫的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!