为什么私有方法也不能是最终的? [英] Why private method can not be final as well?
问题描述
将 private
和 final
添加到同一个方法是否多余?
Is it redundant to add private
and final
to a same method?
class SomeClass {
//--snip--
private final void doStuff()
{
// private work here
}
}
如果它是private
,那么任何人都无法覆盖它,对吧?
If it's private
, there's no way anyone can override it, right?
如果没有效果,为什么可以添加final
关键字?(或者我错过了什么?)
Why is it possible to add final
keyword if it has no effect? (or am I missing something?)
推荐答案
基本上是允许的,因为他们觉得放置一个特殊情况来禁止 private
修饰符是不值得的.这就像您还可以将接口上的方法声明为 public
,或将接口中的嵌套类声明为 static
,即使这些关键字隐含在接口中.您还可以在 final
类等上声明 final
方法.
Basically, it's allowed because they didn't feel like it's worthwhile to put a special case prohibiting the private
modifier. It's like how you can also declare methods on an interface as public
, or nested classes in an interface as static
, even though those keywords are implied in interfaces. You can also declare final
methods on a final
class, etc.
当您添加多余的修饰符时,Java 采取了不抱怨的立场.他们始终如一地这样做.
Java took the stance of not complaining when you add redundant modifiers. They do it consistently.
这篇关于为什么私有方法也不能是最终的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!