派生类如何调用基类的私有方法?
2022-09-01 10:31:29
public class PrivateOverride {
private void f() {
System.out.println("private f()");
}
}
public class Derived extends PrivateOverride {
public void f() { //this method is never run.
System.out.println("public f()");
}
}
public static void main(String[] args) {
// instantiate Derived and assign it to
// object po of type PrivateOverride.
PrivateOverride po = new Derived();
// invoke method f of object po. It
// chooses to run the private method of PrivateOveride
// instead of Derived
po.f();
}
}
因此,此代码的输出为 。现在,我脑海中浮现出一个问题:作为派生类对象的 po 如何调用作为其基类的 PrivateOverride 的私有方法?private f()