[問題] 一個多形的問題

看板java作者 (來呀)時間17年前 (2008/05/04 22:19), 編輯推噓4(400)
留言4則, 4人參與, 最新討論串1/3 (看更多)
4. Given the following Java code: [10 points] 10. interface A { public int getValue();} 11. class B implements A { 12. public int getValue() { return 1;} 13. } 14. class C extends B { 15. // insert code here16. } What three code fragments individually at line 15, make used of polymorphism? (Choose three) (A) public void add (C c) {c.getValue();} (B) public void add (B b) {b.getValue();} (C) public void add (A a) {a.getValue();} (D) public void add (A a, B b) {a.getValue();} (E) public void add (C c1, C c2) {c1.getValue();} ANS:__ (B), (C), (D)__ 請問是否有人可以跟我解釋一下 為什麼答案是BCD嗎 我完全不了解這樣跟多形有什麼關係耶@@ 拜託了~感謝萬分 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 218.168.193.40

05/04 22:59, , 1F
真糟糕>< 我還以為是abe... 我得加強一下觀念了(淚)
05/04 22:59, 1F

05/05 00:22, , 2F
直觀啊,參數型態沒有 C 的就是 BCD
05/05 00:22, 2F

05/05 22:59, , 3F
因為只有 BCD 可以在 runtime 時期決定parameter型別
05/05 22:59, 3F

05/06 16:20, , 4F
因為C沒有實作getValue() interface,是由其父類別實作
05/06 16:20, 4F
文章代碼(AID): #187SNf2_ (java)
文章代碼(AID): #187SNf2_ (java)