JAVA學(xué)習(xí)繼承:繼承中的方法的覆蓋和重載
若函數(shù)和父類(lèi)同名,參數(shù)相同則覆蓋,否則為重載
public?class?Test11A{ ????public?void?somoke() ????{ ????????System.out.println("chouyan"); ????} }
public?class?Test11B?extends?Test11A{ ????public?void?somoke()//覆蓋父類(lèi)的somoke方法 ????{ ????????System.out.println("bu?chou?yan"); ????} ????public?static?void?main(String[]?args)?{ ????????Test11B?test??=?new?Test11B(); ????????test.somoke(); ????} }
重載:
public?class?Test11B?extends?Test11A{ ????public?void?somoke(String??s)//參數(shù)不同,會(huì)重載父類(lèi)的方法 ????{ ????????System.out.println(s); ????} ????public?static?void?main(String[]?args)?{ ????????Test11B?test??=?new?Test11B(); ????????test.somoke(); ?????????test.somoke("hello"); ????} }