Java教程说我可以有一个包私有接口,但我不能
在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
接口本身可以是包私有的,而不是其中的方法。您可以定义一个只能在定义它的包中使用(按名称)的接口,但它的方法像所有接口方法一样是
public
的。如果一个类实现了该接口,那么它定义的方法必须是public
。这里的关键是接口类型在包外部不可见,而不是方法。这些文档没有不正确,因为使用接口中定义的方法与使用接口本身不同。另请注意,定义接口时,在方法定义之前不添加
public
不会改变任何内容,因为这些方法都是隐式public
的。如果您实现接口的类本身是包私有的,那么接口方法的公共性显然不是问题。当然,如果单继承问题不妨碍您,您也可以使用抽象类而不是接口:
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 bepublic
. 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 implicitlypublic
.If the class(es) that you have implementing the interface are themselves package-private, the
public
ness 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:对我来说解决单继承问题的方法是:
我没有使用 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.
我认为(尽管我可能是错的)这里讨论的较弱访问权限是针对
foo()
和bar()
NewClass
中的方法。所有接口方法都隐式地public
,但在NewClass
中,您将它们保留为包私有的,这比public
的保证更弱。将NewClass
更改为可能将解决此问题。
I think (though I could be wrong about this) that the weaker access privileges being discussed here are for the
foo()
andbar()
methods inNewClass
. All interface methods are implicitlypublic
, but inNewClass
you've left them package-private, which is a weaker guarantee thanpublic
. ChangingNewClass
to readprobably will fix this.