Java默认访问级别(程序包私有访问)。为什么用于? [英] Java default access level (package private access). Why it is used for?
问题描述
尽管我知道默认(程序包)访问级别在Java中是如何工作的,但我无法想象它在应用程序中的真正用途,除了那些具有唯一程序包的程序。您在商务(通常是多包)java应用程序中使用默认访问级别是什么?
Although I know how default (package) access level works in java, I can't imagine its real use in applications except for those with unique package. What do you use default access level for in business (usually multipackage) java applications?
推荐答案
使用包的一种非常常见的模式-私有类用于创建接口的 shared 实现或公共类的共享子类。
A very common pattern for using package-private classes is to create shared implementations of interfaces or shared subclasses of common classes.
请注意,共享的事实在这里很重要,因为内部类为向调用者隐藏类/接口实现提供了更好的选择。
Note that the fact of sharing is important here, because inner classes provide a better alternative for hiding class / interface implementations from the caller.
另一种常见用法是实用类,当您希望共享一组算法时,水平,而不会将这些算法暴露给您的类库的外部用户。各种参数检查器都属于此类。
Another common use is for "utility classes", when you wish to share a group of algorithms "horizontally" among different classes, without exposing these algorithms to outside users of your class library. Various argument checkers fall into this category.
这篇关于Java默认访问级别(程序包私有访问)。为什么用于?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!