3 回答
已采納

望遠(yuǎn)
TA貢獻(xiàn)1017條經(jīng)驗(yàn) 獲得超1032個(gè)贊
多態(tài)分為靜態(tài)多態(tài)--在運(yùn)行前就已經(jīng)確定要執(zhí)行的代碼塊,體現(xiàn)在代碼中就是方法的重載;
public?class?Test{ ????public?void?say(){ ????System.out.println("Hello?World"); ????} ????public?void?say(String?s){ ????System.out.println(s); ????} ????public?static?void?main(String[]?args){ ????????new?Test().say();//在運(yùn)行前確定了要執(zhí)行的代碼塊 ????????//new?Test().say("Hello?World"); ????} }
動(dòng)態(tài)多態(tài)--在運(yùn)行前無法確定要執(zhí)行的代碼塊具體是什么,體現(xiàn)在代碼中就是接口回調(diào)和父類對(duì)象引用指向子類對(duì)象
public?class?Animal{ ????public?void?say(){ ????System.out.println("動(dòng)物會(huì)叫"); ????} } class?Dog?extends?Animal{ ????public?void?say(){ ????????System.out.println("汪汪汪~~"); ????} } class?Snake?extends?Animal{ ????public?void?say(){ ????????System.out.println("嘶嘶嘶~~"); ????} } class?Test{ ????public?void?test(Animal?animal){ ????????????animal.say(); ????????} ????public?static?void?main(){ ????????new?Test().test(new?Animal());//輸出動(dòng)物會(huì)叫 ????????//new?Test().test(new?Dog());//多態(tài),輸出汪汪汪 ????????//new?Test().test(new?Snake());//多態(tài),輸出嘶嘶嘶 ????} }

biubiubong
TA貢獻(xiàn)3條經(jīng)驗(yàn) 獲得超0個(gè)贊
在同一個(gè)方法中,由于參數(shù)類型不同導(dǎo)致執(zhí)行結(jié)果不同的現(xiàn)象就是多態(tài)。
添加回答
舉報(bào)
0/150
提交
取消