看板 java 關於我們 聯絡資訊
最近遇到一個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
PsMonkey:JLS 8.4.8.3 or http://bit.ly/wTRgkw 01/12 04:06
ssc24:private method 只有同一class能看得到 看不到的就不能覆寫 01/12 17:20
chihcai:都private怎麼override 在做override還是多用@Override 02/28 00:28