[問題] override private method

看板java作者 (32767)時間14年前 (2012/01/12 02:38), 編輯推噓0(003)
留言3則, 3人參與, 最新討論串1/1
最近遇到一個override的問題想不出來 原本的程式這樣的 public class Test { public static void main(String[] args) { Base base = new Derived(); base.callFoo(); Derived derived = new Derived(); derived.callFoo(); } } class Base { public void callFoo() { foo(); } public void foo() { System.out.println("Base foo()"); } } class Derived extends Base { public void foo() { System.out.println("Derived foo()"); } } 輸出是:Derived foo() Derived foo() 這邊都跟我觀念上的認知一樣 但是如果把兩個 class 的 foo() 改成 private 的話 輸出就變成:Base foo() Base foo() 為什麼會變成這樣,不是結果應該會跟原本一樣嗎? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.116.191.85

01/12 04:06, , 1F
JLS 8.4.8.3 or http://bit.ly/wTRgkw
01/12 04:06, 1F

01/12 17:20, , 2F
private method 只有同一class能看得到 看不到的就不能覆寫
01/12 17:20, 2F

02/28 00:28, , 3F
都private怎麼override 在做override還是多用@Override
02/28 00:28, 3F
文章代碼(AID): #1F3TSAwL (java)