如
http://docs.oracle.com/javase/tutorial/java/IandI/override.html所示,Java允许
>通过实例方法覆盖实例方法
>通过静态方法隐藏静态方法
我的问题是为什么Java不允许通过实例方法隐藏静态超类方法.这可以这样做:
class Base { static void foo () {} } class Derived extends Base { void foo () {} void access () { foo (); Base.foo (); } }
我没有看到上述方法的任何特殊问题 – 它只是作为(允许)隐藏的静态已经是“混乱/复杂”.
解决方法
我怀疑是要避免与处理基类混淆.事实上,我想象设计师没有看到应该行之有效的明显方式.
class Base { static void foo () {} } class Derived extends Base { void foo () {} // say this compiled } Base b = new Derived() b.foo(); // should the static or the virtual method be called?
b.foo()调用Base.foo()还是应该调用Derived.foo()?