Java 中的一个接口可以有且只能有一个 public 修饰符。为什么其余的不允许?

发布于 2024-12-13 23:15:52 字数 656 浏览 2 评论 0原文

可能的重复:
在接口中受保护

下面的代码片段显示,Java 中的接口只能有一个修饰符,即 公开。接口中不允许使用其他修饰符(私有和受保护),无论是字段还是任何方法。显然,在修饰符中,private 在接口中没有任何意义,但在接口中应该允许使用 protected,因为它可以由其实现类合并。

interface Demo
{
    private final static int a=10; //Will not be compiled.
    protected final static int b=20; //Will not be compiled.
    public final static int x=0;   //ok

    abstract public void showSum();
}

而抽象类允许拥有所有修饰符 private、public 和 protected。我的问题只是,在似乎允许的接口中不允许使用受保护的修饰符。为什么?

Possible Duplicate:
Protected in Interfaces

The following code snippet shows that an interface in Java can have only one modifier which is public. No other modifiers (private and protected) are allowed within an interface neither with fields nor with any methods. Obviously among modifiers, private has no meaning to use within an interface but protected should be allowed within an interface because it can be incorporated by it's implementing class.

interface Demo
{
    private final static int a=10; //Will not be compiled.
    protected final static int b=20; //Will not be compiled.
    public final static int x=0;   //ok

    abstract public void showSum();
}

while an abstract class is allowed to have all the modifiers private, public and protected. My question is only that a protected modifier is not allowed within an interface that somewhat seems to be allowed. Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

惯饮孤独 2024-12-20 23:15:52

显然,最好的答案是“因为他们就是这样定义的”。我认为我不会太仔细地研究 Java 最初定义时做出的决策背后的基本原理;那是很久以前的事了,现在,使用该语言获得的经验表明,许多最初的决定都是有缺陷的。

在这种情况下,接口旨在充当与对象通信的公共协议,因此决定所有成员都必须是公共的。这可能不是最好或最有用的定义,但它是我们所拥有的,我们必须接受它。

Obviously the best answer is "because that's how they defined it." I don't think I'd look too hard at the rationale behind decisions made when Java was initially defined; it was a long time ago, now, and experience gained by using the language has shown that many of those initial decisions were flawed.

In this case, an interface is intended to serve as a public protocol for communicating with an object, and as such, it was decided that all members must be public. This may not have been the best or most useful definition, but it's the one we have, and we have to live with it.

终陌 2024-12-20 23:15:52

接口中的所有方法和字段都应该以这样的方式声明,以便可以从任何地方调用它们。不仅来自子类内部。

只有 public 修饰符才能实现这一点。

但是,应该避免在接口中存在字段。如果可能的话。

All methods and fields in an interface should be declared in such a way so that they can be invoked from anywhere. Not only from within a subclass.

Only public modifier can make this happen.

However, it should be avoided to have a field in interface. if possible.

以可爱出名 2024-12-20 23:15:52

抽象类提供了子类继承的一些实现。

接口只是定义了一个外部API,而不提供任何实现。接口背后的整个思想是实现完全留给实现类。

An abstract class provides some implementation that subclasses inherit.

An interface simply defines an external API without providing any implementation. The whole idea behind an interface is that the implementation is left entirely to the implementing class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文