As shown in http://docs.oracle.com/javase/tutorial/java/IandI/override.html, Java does allow
如http://docs.oracle.com/javase/tutorial/java/IandI/override.html所示,Java是允许的
- Overriding an instance method by an instance method and
- 用实例方法重写实例方法。
- Hiding a static method by a static method
- 用静态方法隐藏静态方法
My question is why Java doesn't allow hiding a static superclass method by an instance method. This could be done like this:
我的问题是为什么Java不允许用实例方法隐藏静态超类方法。可以这样做:
class Base {
static void foo () {}
}
class Derived extends Base {
void foo () {}
void access () {
foo ();
Base.foo ();
}
}
I don't see any particular issue with the above approach - it is only as "messy/complex" as the (allowed) hiding of statics already is.
我不认为上述方法有什么特别的问题——它只是像(允许的)静态隐藏一样“混乱/复杂”。
4 个解决方案
#1
12
I suspect it is to avoid confusion with dealing with the base class. In fact I imagine the designers didn't see an obvious way this should behave.
我怀疑这是为了避免处理基类时的混乱。事实上,我想设计师们并没有看到这种行为的明显方式。
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?
Should b.foo() call Base.foo() or should it potentially call Derived.foo()?
b.foo()应该调用Base.foo(),还是应该调用Derived.foo()?
#2
5
Simple answer: that would be the mess.
简单的回答是:那将是一团糟。
Concrete answer: what to call in that case Derived.foo()
? Base.foo()
can't be called as it's hidden (as per you), Derived.foo()
can't be called as it's not static.
具体答案:在这种情况下,调用什么?Base.foo()不能被调用,因为它是隐藏的(根据您的要求),Derived.foo()不能被调用,因为它不是静态的。
#3
1
Because, one are like Bananas and the other ones are Apples.
因为,一个像香蕉,另一个像苹果。
Explaination:
解释:
- Static Methods are created when reading the Class-Structure
- 静态方法是在读取类结构时创建的。
- Methods are created when a object of a class is created.
- 方法是在创建类的对象时创建的。
Example:
例子:
Foo.bar();
is something different than
是不同于
new Foo().bar();
Guess which one is called?
猜猜叫什么名字?
Foo f = new Foo();
f.bar();
#4
1
Another to add here is: 1. Static methods belong at the class level. So u cannot override method in the derived class. as simple its called hiding. :) 2. Instance methods belong to the objects, so objects are overrided. So we can override in the derived class.
另一个要加的是:1。静态方法属于类级别。所以u不能重写派生类中的方法。简单地说就是隐藏。:2)。实例方法属于对象,因此对象被覆盖。我们可以重写派生类。
Above other comments give a good example have a look into it..
上面的其他评论给出了一个很好的例子。
Regards Punith
问候Punith
#1
12
I suspect it is to avoid confusion with dealing with the base class. In fact I imagine the designers didn't see an obvious way this should behave.
我怀疑这是为了避免处理基类时的混乱。事实上,我想设计师们并没有看到这种行为的明显方式。
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?
Should b.foo() call Base.foo() or should it potentially call Derived.foo()?
b.foo()应该调用Base.foo(),还是应该调用Derived.foo()?
#2
5
Simple answer: that would be the mess.
简单的回答是:那将是一团糟。
Concrete answer: what to call in that case Derived.foo()
? Base.foo()
can't be called as it's hidden (as per you), Derived.foo()
can't be called as it's not static.
具体答案:在这种情况下,调用什么?Base.foo()不能被调用,因为它是隐藏的(根据您的要求),Derived.foo()不能被调用,因为它不是静态的。
#3
1
Because, one are like Bananas and the other ones are Apples.
因为,一个像香蕉,另一个像苹果。
Explaination:
解释:
- Static Methods are created when reading the Class-Structure
- 静态方法是在读取类结构时创建的。
- Methods are created when a object of a class is created.
- 方法是在创建类的对象时创建的。
Example:
例子:
Foo.bar();
is something different than
是不同于
new Foo().bar();
Guess which one is called?
猜猜叫什么名字?
Foo f = new Foo();
f.bar();
#4
1
Another to add here is: 1. Static methods belong at the class level. So u cannot override method in the derived class. as simple its called hiding. :) 2. Instance methods belong to the objects, so objects are overrided. So we can override in the derived class.
另一个要加的是:1。静态方法属于类级别。所以u不能重写派生类中的方法。简单地说就是隐藏。:2)。实例方法属于对象,因此对象被覆盖。我们可以重写派生类。
Above other comments give a good example have a look into it..
上面的其他评论给出了一个很好的例子。
Regards Punith
问候Punith