为什么私有方法也不能最终确定? [英] 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屋!