我什么时候会在 Java 中使用 package-private?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18382934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:04:41  来源:igfitidea点击:

When would I use package-private in Java?

javaprivateprotectedaccess-controlpackage-private

提问by NobleUplift

I love access control in any language, but I find that in Java I almost never (if ever) use the package-privateaccess modifier (or lack thereof).

我喜欢任何语言的访问控制,但我发现在 Java 中我几乎从不(如果有的话)使用包私有访问修饰符(或缺少它)。

I realize that inner classes can be private, protected, or package-private, but outer classes can only be package-privateor public. Why can an outer class be package-privatebut not protected? What is the benefit of restricting classes/methods/fields to be seen by the entire package, but not subclasses?

我意识到内部类可以是private, protected, or package-private,但外部类只能是package-privateor public。为什么外部类可以是package-private但不是protected?限制整个包可以看到类/方法/字段而不是子类有什么好处?

采纳答案by dkatzel

I use package-privateclasses and methods when I want to hide implementation details from users (and other classes) outside the package.

package-private当我想对包外的用户(和其他类)隐藏实现细节时,我会使用类和方法。

For example if I have an interface and a factory class that creates instances of that interface, I may have the implementation class as a separate file but mark it package-private so others can not use it, nor will it clutter the JavaDoc (if javadoc set to only show public).

例如,如果我有一个接口和一个创建该接口实例的工厂类,我可能将实现类作为一个单独的文件,但将其标记为包私有的,这样其他人就不能使用它,也不会弄乱 JavaDoc(如果 javadoc设置为仅公开显示)。

If you seal your jar file, package-private methods can also help restrict who can access these methods. If a method is public or protected, subclasses can still see and call that method even if it's in a different package. (Unsealed jars allow anyone to make classes in your packages so they will get access to package-private or protected methods)

如果您密封了 jar 文件,包私有方法也可以帮助限制谁可以访问这些方法。如果一个方法是公共的或受保护的,即使它在不同的包中,子类仍然可以看到和调用该方法。(未密封的 jar 允许任何人在您的包中创建类,以便他们可以访问包私有或受保护的方法)

回答by Daniel S.

In many cases, peer classes in the same package have the same author, thus he knows about the inner way these classes work, or in other words he knows about the encapsulated logic of these classes. Thus he can make sure that package-private accesses between classes adhere to the encapsulated logic of the accessed class and that these accesses do not break anything.

在很多情况下,同一个包中的对等类具有相同的作者,因此他知道这些类的内部工作方式,或者说他知道这些类的封装逻辑。因此,他可以确保类之间的包私有访问遵循被访问类的封装逻辑,并且这些访问不会破坏任何东西。

These direct accesses are often useful for optimizations and for keeping the amount of source code smaller.

这些直接访问通常可用于优化和减少源代码的数量。

For the question part why outer classes may be package-private, but not protected, I have no answer.

对于为什么外部类可能是包私有的,但不受保护的问题部分,我没有答案。