如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 ();
}
}
Run Code Online (Sandbox Code Playgroud)
我没有看到上述方法有任何特殊问题 - 它只是像"允许的"隐藏静态的"混乱/复杂".
Pet*_*rey 12
我怀疑这是为了避免与处理基类混淆.事实上,我想设计师没有看到这应该表现的明显方式.
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?
Run Code Online (Sandbox Code Playgroud)
b.foo()应该调用Base.foo()还是应该调用Derived.foo()?
简单的回答:那将是一团糟.
具体答案:在那种情况下应该Derived.foo()怎么称呼?Base.foo()不能被调用,因为它被隐藏(按照你),Derived.foo()不能被调用,因为它不是静态的.