若函數和父類同名,參數相同則覆蓋,否則為重載
public?class?Test11A{ ????public?void?somoke() ????{ ????????System.out.println("chouyan"); ????} }
public?class?Test11B?extends?Test11A{ ????public?void?somoke()//覆蓋父類的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)//參數不同,會重載父類的方法 ????{ ????????System.out.println(s); ????} ????public?static?void?main(String[]?args)?{ ????????Test11B?test??=?new?Test11B(); ????????test.somoke(); ?????????test.somoke("hello"); ????} }