最终和私有静态
我读到,doing:
public final void foo() {}
等于:
private static void foo() {}
两者都意味着该方法不可重写!
但我看不到如果一个方法是私有的,它就自动不是等价的 可访问...
I read that doing:
public final void foo() {}
is equals to:
private static void foo() {}
both meaning that the method is not overridable!
But I don't see the equivalence if a method is private it's automatically not
accessible...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确实,您不能使用任何一种方法
@Override
。您只能使用@Override
非final
实例方法。final
,那么您就无法@Override
它。static
,那么它就不是实例方法开始它们不真的“相等”,因为一个是
private static
,另一个是public final
。static
上下文中的实例 方法/字段您不能
@Override
static
方法,但可以使用另一个静态方法隐藏它。当然,
static
方法不允许动态分派(这是由@Override
完成的)。参考文献
相关问题
It's true that you can not
@Override
either method. You can only@Override
a non-final
instance method.final
, then there's no way you can@Override
itstatic
, then it's not an instance method to begin withIt's NOT true that they're "equal", because one is
private static
, and the other ispublic final
.static
contextYou can not
@Override
astatic
method, but you can hide it with anotherstatic
method. Astatic
method, of course, does not permit dynamic dispatch (which is what is accomplished by an@Override
).References
Related questions
两者都不能被覆盖,但原因却截然不同。第一个是公共非静态方法,而第二个是静态方法。因此,第一个不可重写只是因为它已被声明为最终的,而第二个是静态的,永远不能被重写。
请注意,从第一个开始,您可以访问类的非静态成员,而从第二个开始,您就不能访问该类的非静态成员。因此它们的使用方式非常不同,因此并不“平等”。
Neither can be overridden, but for very different reasons. The first is a public nonstatic method, while the secod is static. So the first is not overridable only because it has been declared final, while the second, being static, can never be overridden.
Note that from the first you can access nonstatic members of the class, while from second you can't. So they are used in very different ways, thus are not "equal".