[問題] override private method
最近遇到一個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
01/12 04:06, 1F
→
01/12 17:20, , 2F
01/12 17:20, 2F
→
02/28 00:28, , 3F
02/28 00:28, 3F