简体   繁体   English

Java接口 - 为什么没有私有方法?

[英]Java Interfaces — why not have private methods?

Why are methods on interfaces always public? 为什么接口上的方法总是公开的? Why can't they be private? 他们为什么不能私下?

Because all methods on an interface are public. 因为接口上的所有方法都是公共的。 That's the point in having the interface -- technically it defines a contract for your class (which may have many, overlapping, contracts/interfaces). 这就是拥有界面的重点 - 从技术上讲,它为您的类定义了一个合同(可能有许多重叠的合同/接口)。 The client of your class should hold a reference to the interface and have access only to the class' published (public) methods through the interface. 您的类的客户端应该拥有对该接口的引用,并且只能通过该接口访问类的已发布(公共)方法。

I infer that you are referring to an interface declared thusly: 我推断你指的是这样声明的接口:

public interface MyInter 
{
    public void myFunc();
}

And the resulting error if you omit the public qualifier in your implementation: 如果省略实现中的public限定符,则会产生错误:

MyClass.java:3: myFunc() in MyClass cannot implement myFunc() in MyInter; attempting to assign weaker access privileges; was public
    void myFunc(){}
         ^

Say you could make myFunc private. 假设您可以将myFunc私有。 You write the following code in a different class. 您在不同的类中编写以下代码。 This should complain about you trying to use a private function you didn't have access to: 这应该抱怨您尝试使用您无权访问的私有函数:

MyClass foo = new MyClass();
foo.myFunc(); // Declared private, can't call it.

But what about this: 但是这个怎么样:

void doSomething(MyInter foo)
{
    foo.myFunc(); // Declared public in interface, private in implementation.
}

Can we do this? 我们可以这样做吗? According to the interface it's a public method so we should be good to go. 根据界面,它是一种公共方法,所以我们应该好好去。 But it's implemented as a private method so the class expects it never to be called from the outside like this, a restriction that should be enforced by the compiler. 但它是作为一个私有方法实现的,所以该类期望永远不会从外部调用它,这是一个应该由编译器强制执行的限制。 But the compiler doesn't even need to know about MyClass for this to compile. 但是编译器甚至不需要了解MyClass来编译它。 It could not even be written yet, or in an external library that may or may not ever be integrated. 它甚至无法编写,或者在可能会或可能不会集成的外部库中。

Allowing implementations creates an internal inconsistency in the rules of allowable access, and the resolution to that inconsistency is to disallow the situation altogether. 允许实现在允许访问的规则中创建内部不一致,并且解决该不一致是完全不允许这种情况。 Anything that can call a method in an interface must be able to call it in any implementation. 任何可以在接口中调用方法的东西都必须能够在任何实现中调用它。

The same argument holds true for overriding subclass methods. 对于重写子类方法,同样的论点也适用。 You can't "hide" them by overriding with more restrictive qualifiers. 你不能通过覆盖更严格的限定符来“隐藏”它们。

Why is it so? 为什么会这样?

Because the JLS says so: 因为JLS这样说:

In the Chapter on interface declarations, JLS 9.4 says: "Every method declaration in the body of an interface is implicitly public ." 在关于接口声明的章节中, JLS 9.4说: “接口主体中的每个方法声明都是隐式public 。”

In the Chapter on class declarations, JLS 8.4.8.3 says: "The access modifier (§6.6) of an overriding or hiding method must provide at least as much access as the overridden or hidden method, ..." 在关于类声明的章节中, JLS 8.4.8.3说: “覆盖或隐藏方法的访问修饰符(第6.6节)必须至少提供与重写或隐藏方法一样多的访问权限,......”

Engineer Dollery's answer explains why the language is designed this way. 工程师Dollery的答案解释了为什么语言是这样设计的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM