Java 中的接口:无法将实现的方法设置为受保护或私有
我知道接口必须是公共的。 然而,我不想要这样。
我希望我实现的方法只能从它们自己的包中访问,所以我希望我实现的方法受到保护。
问题是我无法保护接口或实现的方法。
什么是解决办法? 有没有与这个问题相关的设计模式?
根据 Java 指南,抽象类也无法完成这项工作。
I know that an interface must be public. However, I don't want that.
I want my implemented methods to only be accessible from their own package, so I want my implemented methods to be protected.
The problem is I can't make the interface or the implemented methods protected.
What is a work around? Is there a design pattern that pertains to this problem?
From the Java guide, an abstract class wouldn't do the job either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
阅读此。
“公共访问说明符表明该接口可以被任何包中的任何类使用。如果您没有指定该接口是公共的,则您的接口将只能被与该接口在同一包中定义的类访问< /强>。”
那是你要的吗?
read this.
"The public access specifier indicates that the interface can be used by any class in any package. 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."
Is that what you want?
您的类可以使用包保护并仍然实现一个接口:
如果您希望某些方法受到保护/包而其他方法则不受到保护/包,那么听起来您的类有多个职责,并且应该分为多个。
阅读对此回复和其他回复的评论后进行编辑:
如果您以某种方式认为方法的可见性会影响调用该方法的能力,请再考虑一下。 如果不走极端,您就无法阻止某人使用反射来识别您的类的方法并调用它们。 然而,这不是问题:除非有人试图破解您的代码,否则他们不会调用随机方法。
相反,将私有/受保护方法视为定义子类的契约,并使用接口来定义与外界的契约。
哦,对于决定我的示例应该使用 K&R 支撑的人:如果服务条款中指定,当然可以。 否则,你就不能找到更好的事情来打发你的时间吗?
You class can use package protection and still implement an interface:
If you want some methods to be protected / package and others not, it sounds like your classes have more than one responsibility, and should be split into multiple.
Edit after reading comments to this and other responses:
If your are somehow thinking that the visibility of a method affects the ability to invoke that method, think again. Without going to extremes, you cannot prevent someone from using reflection to identify your class' methods and invoke them. However, this is a non-issue: unless someone is trying to crack your code, they're not going to invoke random methods.
Instead, think of private / protected methods as defining a contract for subclasses, and use interfaces to define the contract with the outside world.
Oh, and to the person who decided my example should use K&R bracing: if it's specified in the Terms of Service, sure. Otherwise, can't you find anything better to do with your time?
当我遇到这个问题时,我使用包可访问的内部或嵌套类来实现接口,将实现的方法从公共类中推出。
通常是因为我有一个具有特定公共 API 的类,它必须实现其他东西才能完成它的工作(通常是因为其他东西是伪装成接口的回调) - 这种情况在 Comparable 之类的事情上经常发生。 我不希望公共 API 被(强制公共)接口实现所污染。
希望这可以帮助。
另外,如果您确实想要仅由包访问的方法,则不需要 protected 范围说明符,您需要默认(省略)范围说明符。 当然,使用 protected 将允许子类看到这些方法。
顺便说一句,我认为接口方法被推断为公共的原因是因为拥有一个仅由同一包中的类实现的接口是非常例外的; 它们最常被另一个包中的某些东西调用,这意味着它们需要公开。
When I have butted up against this I use a package accessible inner or nested class to implement the interface, pushing the implemented method out of the public class.
Usually it's because I have a class with a specific public API which must implement something else to get it's job done (quite often because the something else was a callback disguised as an interface <grin>) - this happens a lot with things like Comparable. I don't want the public API polluted with the (forced public) interface implementation.
Hope this helps.
Also, if you truly want the methods accessed only by the package, you don't want the protected scope specifier, you want the default (omitted) scope specifier. Using protected will, of course, allow subclasses to see the methods.
BTW, I think that the reason interface methods are inferred to be public is because it is very much the exception to have an interface which is only implemented by classes in the same package; they are very much most often invoked by something in another package, which means they need to be public.
这个问题是基于一个错误的陈述:
事实并非如此,您可以拥有带有默认访问修饰符的接口。
这里是:
因此,实现你想要的,防止在包之外使用接口和类。
This question is based on a wrong statement:
Not really, you can have interfaces with default access modifier.
Here it is:
Hence, achieving what you wanted, prevent interface and class usage outside of the package.
以下是如何使用抽象类来完成此操作。
唯一不方便的是它使你成为“子类”。
根据 java 指南,您应该“大多数”时候遵循该建议,但我认为在这种情况下就可以了。
Here's how it could be done using abstract classes.
The only inconvenient is that it makes you "subclass".
As per the java guide, you should follow that advice "most" of the times, but I think in this situation it will be ok.
这是另一个解决方案,其灵感来自于 C++ Pimpl 习惯用法。
如果您想要实现一个接口,但不希望该实现是公共的,则可以创建一个实现该接口的匿名内部类的组合对象。
这是一个例子。 假设您有这个接口:
您创建一个
Iface
类型的对象,并将实现放入其中:每当您需要调用
doSomething()
时,您就可以在您的组合impl
对象。Here's another solution, inspired by the C++ Pimpl idiom.
If you want to implement an interface, but don't want that implementation to be public, you can create a composed object of an anonymous inner class that implements the interface.
Here's an example. Let's say you have this interface:
You create an object of the
Iface
type, and put your implementation in there:Whenever you need to invoke
doSomething()
, you invoke it on your composedimpl
object.我刚刚遇到这个问题,试图构建一个受保护的方法,其目的是仅在测试用例中使用它。 我想删除填充到数据库表中的测试数据。 无论如何,我的灵感来自 @Karl Giesing 的 帖子。 不幸的是它没有起作用。 我确实找到了一种使用受保护的内部类使其工作的方法。
接口:
然后在公共类中定义为受保护的内部类:
然后您只能从同一包中的其他类访问受保护的方法,正如我的测试代码所示:
对于原始海报来说有点晚了,但是嘿,我刚刚找到了。 :D
I just came across this trying to build a protected method with the intention of it only being used in a test case. I wanted to delete test data that I had stuffed into a DB table. In any case I was inspired by @Karl Giesing's post. Unfortunately it did not work. I did figure a way to make it work using a protected inner class.
The interface:
Then the inner class defined as protected in public class:
You can then access the protected method only from other classes in the same package, as my test code was as show:
A little late for the original poster, but hey, I just found it. :D
您可以使用封装而不是继承。
也就是说,创建您的类(不会继承任何内容)并在其中包含您要扩展的对象的实例。
然后你就可以只公开你想要的内容。
这样做的明显缺点是您必须显式传递您想要公开的所有内容的方法。 而且它不会是一个子类......
You can go with encapsulation instead of inheritance.
That is, create your class (which won't inherit anything) and in it, have an instance of the object you want to extend.
Then you can expose only what you want.
The obvious disadvantage of this is that you must explicitly pass-through methods for everything you want exposed. And it won't be a subclass...
我只想创建一个抽象类。 这没有什么坏处。
I would just create an abstract class. There is no harm in it.
对于接口,您希望定义可以由各种实现类公开的方法。
拥有一个带有受保护方法的接口并不能达到这个目的。
我猜你的问题可以通过重新设计你的类层次结构来解决。
With an interface you want to define methods that can be exposed by a variety of implementing classes.
Having an interface with protected methods just wouldn't serve that purpose.
I am guessing your problem can be solved by redesigning your class hierarchy.
解决这个问题的一种方法是(根据情况)创建一个匿名内部类来实现具有受保护或私有范围的接口。 例如:
然后在
Foo
的用户中:这可以让您避免出现以下情况:
One way to get around this is (depending on the situation) to just make an anonymous inner class that implements the interface that has
protected
orprivate
scope. For example:Then in the user of
Foo
:This saves you from having the following:
我想你现在可以在 Java 9 版本中使用它。 来自 Java 9 的 openJDK 注释,
参考 https://bugs.openjdk.java.net/browse/JDK-8071453
I think u can use it now with Java 9 release. From the openJdk notes for Java 9,
refer https://bugs.openjdk.java.net/browse/JDK-8071453