为什么私有方法是最终的? [英] Why can private method be final?

查看:168
本文介绍了为什么私有方法是最终的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么这是好的:

  class SomeClass {

// - -snip--

private final void doStuff()
{
// private work here
}
}



如果是私有的,任何人都不能覆盖它,对吗?



如果没有效果,为什么可以添加 final 关键字? (或者我缺少一些东西?)

解决方案

基本上,它是允许的,因为他们不觉得有必要case禁止 private 修饰符。这就像你也可以在接口上声明方法 public ,或者在接口中嵌套类 static 尽管这些关键字隐含在接口中。您还可以在最终类上声明 final 方法。



当您添加冗余修饰符时,Java采取了不抱怨的立场。他们一贯这样做。


I'm wondering why this is okay:

class SomeClass {

    //--snip--

    private final void doStuff()
    {
        // private work here
    }
}

If it's private, there's no way anyone can override it, right?

Why is it possible to add final keyword if it has no effect? (or am I missing something?)

解决方案

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 took the stance of not complaining when you add redundant modifiers. They do it consistently.

这篇关于为什么私有方法是最终的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆