简体   繁体   English

为什么Java中的“protected”修饰符允许访问同一个包中的其他类?

[英]Why does the “protected” modifier in Java allow access to other classes in same package?

What is the reason that in Java, a member with a "protected" modifier can not only be accessed by the same class and by subclasses, but also by everyone in the same package? 在Java中,具有“受保护”修饰符的成员不仅可以由同一个类和子类访问,还可以由同一个包中的每个人访问?

I am wondering about language design reasons, not actual applications (eg, testing) 我想知道语言设计的原因,而不是实际的应用程序(例如,测试)

This design is based on the idea that the package is the appropriate unit, maintained and released by one internally consistent team; 这种设计基于这样的想法:包装是适当的单元,由一个内部一致的团队维护和发布; inheritance relationships have much less to do with who's maintaining and releasing what when. 继承关系与谁维护和释放什么时候关系不大。

The modifiers are well-described at http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html . 修饰符在http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html中有详细描述。 From there we see this figure. 从那里我们看到这个数字。

Modifier        Class     Package   Subclass  World
public          Y         Y         Y         Y
protected       Y         Y         Y         N
no modifier     Y         Y         N         N
private         Y         N         N         N

From this the reason for the design decision is obvious: it's to have a nice symmetric matrix. 由此可见设计决策的原因很明显:它有一个很好的对称矩阵。

In Java 1.0 there was a fifth access modifier: private protected . 在Java 1.0中,有第五个访问修饰符: private protected This was protected without the default access. 没有默认访问权限就可以protected Apparently it never actually worked properly and was dropped in 1.1. 显然它从来没有真正正常工作,并在1.1中被删除。 So it looks like claims the protected is defined the way it is for total ordering appear to be spurious. 因此看起来像声明protected的定义方式总排序看起来是虚假的。 ( Edit: It does appear that at least one of the reasons the fifth access modifier was removed in 1.1 was that the lack of total ordering interfered with the overload selection rules.) The module access modifier in Java 7 has a few design questions in this area. 编辑:看来,在1.1中删除第五个访问修饰符的原因中至少有一个原因是缺少总排序干扰了重载选择规则。)Java 7中的module访问修饰符有一些设计问题。区域。

Given that it was thought that it would be a good idea for the default access modifier for members to be "package private", it seems reasonable that protected should have be at least this level of access. 鉴于认为成员的默认访问修饰符为“包私有”是个好主意,所以protected应至少具有这种访问级别似乎是合理的。 For my money, protected doesn't pay its way in the language at all. 对于我的钱, protected根本不会用语言支付费用。

Basically it has to do with the view of a package as an api controlled unit (hence the recommendation to start your package with your domain name - guaranteed global uniqueness), so visibility grows from private -> package-private -> protected -> public. 基本上它与包作为api控制单元的视图有关(因此建议使用您的域名启动您的包 - 保证全局唯一性),因此可见性从私有 - > package-private - > protected - > public 。 If protected weren't an increase over package-private, rather a different type of visibility, there would have to be some way to combine the two types of visibility when needed. 如果受保护不是增加包私有,而是增加不同类型的可见性,则必须有某种方法在需要时组合两种类型的可见性。

Given progressive levels of access, private, package, protected and public, it would be unnecessarily limiting if it went protected then package since that would force me to allow subclasses access in order to grant other members of the same package. 鉴于渐进级别的访问,私有,包,受保护和公共,如果它受到保护然后打包将会不必要地限制,因为这将迫使我允许子类访问以便授予同一包的其他成员。 Yet, intuitively, it should be that other classes in the same package are more trustworthy than other classes "out there". 然而,直观地说,应该是同一个包中的其他类比“那里”的其他类更值得信赖。 So protected is between package and public in that it allows a wider exposure of access. 所以在包和公共之间受到保护,因为它允许更广泛的访问。

I think the basic reason relies on the intuition that there's a basic level of "trust" between classes in the same package; 我认为基本原因依赖于直觉,即同一个包中的类之间存在基本的“信任”级别; you can reasonably expect them to do the right thing with each other - in most cases the package will be the responsibility of a single engineer or team so there should be a consistent harmony of design. 你可以合理地期望他们彼此做正确的事 - 在大多数情况下,包将由一个工程师或团队负责,所以应该有一致的设计和谐。

Java does follow its design principles on itself. Java确实遵循其设计原则。 What happens when you try to reduce/narrow the scope of public method in a subclass ? 当您尝试减少/缩小子类中公共方法的范围时会发生什么? one gets an error. 一个人得到一个错误。 Java scope modifiers levels follow : private < (default) < protected < public Java范围修饰符级别如下:private <(默认)<protected <public

All class in package are supposed to be friendly because they work together. 包中的所有类都应该是友好的,因为它们一起工作。 To make a member available in package it is defined in default scope. 要使成员在包中可用,它将在默认范围内定义。

A subclass may reside outside the package, following scope levels again : private < (default) < protected < public - We can not narrow down the scope. 子类可以驻留在包外部,再次遵循范围级别:private <(默认)<protected <public - 我们无法缩小范围。 Protected is broader scope than default so Java does not contradicts its own guidelines . 受保护的范围比默认范围更广,因此Java与其自身的指导方针不矛盾 Thus, a protected member will be available in default scope. 因此,受保护的成员将在默认范围内可用。 Also : class < package < Project. 另外:class <package <Project。

Please do not limit modifiers to only visibility, but inheritance, structure are also in work at same time and add them to picture as well. 请不要将修饰符限制为仅可见性,但继承,结构也同时工作并将它们添加到图片中。 If this was true : private < protected < (default) < public. 如果是这样的话:private <protected <(默认)<public。 then all sub classes would have to reside in same package, then why you need to inherit you can access everything as default scope is there at applicable at package level. 那么所有子类都必须驻留在同一个包中,那么为什么你需要继承你可以访问所有东西,因为默认范围适用于包级别。 Default scope would have lost its value and so does inheritance. 默认范围将失去其值,继承也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM