Java教程说我可以有一个包私有接口,但我不能

发布于 2024-10-13 23:00:00 字数 508 浏览 9 评论 0原文

Java教程“定义接口”中,它说

如果您没有指定该接口是public,则您的接口将只能由与该接口在同一包中定义的类访问。

但是,这

interface PPInterface {
    void foo();
    void bar();
}

class NewClass implements PPInterface {
    void foo() {}
    void bar() {}
}

会在 NewClass 中生成编译器错误,因为我“试图分配较弱的访问权限;是公开的”。那么文档是错误的,或者我做错了什么,或者我误解了文档?

我想我不必使用界面——我喜欢它,因为它让事情井井有条。

In the Java tutorial "Defining an Interface", it says

If you do not specify that the interface is public, your interface will be accessible only to classes defined in the same package as the interface.

However, this

interface PPInterface {
    void foo();
    void bar();
}

class NewClass implements PPInterface {
    void foo() {}
    void bar() {}
}

generates compiler errors in NewClass because I am 'attempting to assign weaker access privileges; was public'. So the documentation is wrong, or I did something wrong, or I misinterpreted the documentation?

I suppose I don't have to use an interface-- I like it because it keeps things nicely organized.

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

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

发布评论

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

评论(3

給妳壹絲溫柔 2024-10-20 23:00:00

接口本身可以是包私有的,而不是其中的方法。您可以定义一个只能在定义它的包中使用(按名称)的接口,但它的方法像所有接口方法一样是 public 的。如果一个类实现了该接口,那么它定义的方法必须是public。这里的关键是接口类型在包外部不可见,而不是方法。这些文档没有不正确,因为使用接口中定义的方法与使用接口本身不同。

另请注意,定义接口时,在方法定义之前不添加 public 不会改变任何内容,因为这些方法都是隐式 public 的。

如果您实现接口的类本身是包私有的,那么接口方法的公共性显然不是问题。当然,如果单继承问题不妨碍您,您也可以使用抽象类而不是接口:

abstract class Whatever {
  abstract void foo();
  abstract void bar();
}

It's the interface itself that can be package-private, not the methods in it. You can define an interface that can only be used (by name) within the package it's defined in, but its methods are public like all interface methods. If a class implements that interface, the methods it defines must be public. The key thing here is that it's the interface type that isn't visible outside the package, not the methods. The docs are not incorrect, because using the methods defined in the interface is not the same as using the interface itself.

Also be aware that when defining an interface, not adding public before a method definition doesn't change anything since the methods are all implicitly public.

If the class(es) that you have implementing the interface are themselves package-private, the publicness of the interface methods is obviously not an issue. You could also, of course, use an abstract class instead of an interface if the single-inheritance issue doesn't get in your way:

abstract class Whatever {
  abstract void foo();
  abstract void bar();
}
血之狂魔 2024-10-20 23:00:00

对我来说解决单继承问题的方法是:

我没有使用 A 扩展 B 实现 C,而是

使用抽象 D(C 中的包保护接口)扩展 B

,然后 A 扩展 D

工作正常。老实说,也很干净。

what worked for me to get around the single-inheritance problem:

Instead of A extends B implements C

I have abstract D (package protected interface in C) extends B

and then A extends D

Works fine. Clean, too, tbh.

相守太难 2024-10-20 23:00:00

认为(尽管我可能是错的)这里讨论的较弱访问权限是针对 foo()bar() NewClass 中的方法。所有接口方法都隐式地public,但在NewClass 中,您将它们保留为包私有的,这比public 的保证更弱。将 NewClass 更改为

class NewClass implements PPInterface{
    public void foo() {}
    public void bar() {}
}

可能将解决此问题。

I think (though I could be wrong about this) that the weaker access privileges being discussed here are for the foo() and bar() methods in NewClass. All interface methods are implicitly public, but in NewClass you've left them package-private, which is a weaker guarantee than public. Changing NewClass to read

class NewClass implements PPInterface{
    public void foo() {}
    public void bar() {}
}

probably will fix this.

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