Java 是否有 "private protected" 访问修饰符?

Does Java have a "private protected" access modifier?

我看到一些参考文献提到了 Java 中的访问修饰符,称为 private protected(两个词一起):

private protected someMethod() {

}

我找到的其中一个页面是 here。我的学校课程也提到了这个访问修饰符(并说它存在)。但是,使用它会导致 Java 语言出错。

我尝试了变量和方法,我很确定它不存在,但我想要解释发生了什么。考虑过,然后拒绝了吗?还是它在 Java 的较新版本中被删除了?

编辑:我不是在寻找有关 protected 关键字的信息。

删除访问修饰符

Java 最初确实有 private protected 修饰符,但在 JDK 1.0.2(第一个 stable 版本,我们今天知道的 Java 1.0)。关于 JDK 1.0.2 (here and here) 的一些教程如下:

Note: The 1.0 release of the Java language supported five access levels: the four listed above plus private protected. The private protected access level is not supported in versions of Java higher than 1.0; you should no longer be using it in your Java programs.

另一个 answer on SoftwareEngineering.SE 指出:

Java originally had such a modifier. It was written private protected but removed in Java 1.0.

现在看看 Java Version History:

JDK 1.0

The first version was released on January 23, 1996 and called Oak. The first stable version, JDK 1.0.2, is called Java 1.

由此,我们可以得出结论,关于版本 1.0.2 的教程指的是第一个版本,JDK 1.0,该语言称为 Oak,但来自 SoftwareEngineering.SE 的教程指的是第一个稳定版本 JDK 1.0.2 称为 Java 1.0,它已被删除。

现在,如果您尝试在 Java 1.0 documentation 中搜索它,您将找不到它,因为如前所述,它已在 JDK 1.0.2 中删除,也称为 Java 1.0。当您查看您发布的 link 的“上次修改”时间时,再次证明了这一点。您发布的 link 最后一次修改是在 1996 年 2 月。Java 1.0/JDK 1.0.2 在 private protected 被删除后发布 2 月之后1996 年 ,根据规范,1996 年 8 月。

移除原因

一些来源也解释了 private protected 的原因,例如 this 一个。引用:

What was private protected?

Early on, the Java language allowed for certain combinations of modifiers, one of which was private protected. The meaning of private protected was to limit visibility strictly to subclasses (and remove package access). This was later deemed somewhat inconsistent and overly complex and is no longer supported.[5]

[5] The meaning of the protected modifier changed in the Beta2 release of Java, and the private protected combination appeared at the same time. They patched some potential security holes, but confused many people.

并且 SoftwareEngineering.SE 也支持这一点,说它不值得不一致和额外的复杂性,所以它很早就被删除了。

解读

我对这一切的解释是,也许在橡树时代,两者都被允许共存(因此组合)。由于 protected 的含义已更改 1,因此可能需要同时允许 privateprotected。介绍变得太复杂,不值得,所以最后放弃了。到 Java 1.0/JDK 1.0.2 推出时,它已被删除,因此无法在文档中找到。


1Oak Language Specification,Section 4.10,Access to Variables and Methods,注意默认修饰符是 protected:

By default all variables and methods in a class are protected.

这与我们今天的默认包访问方式大不相同。这可能为 private protected 的需求铺平了道路,因为 private 过于严格而 protected 过于宽松。

有 confusing/unclear 个故事:

一个,来自您提供的普林斯顿来源,也来自 MIT archives,指出:

Note: The 1.0 release of the Java language supported five access levels: the four listed above plus private protected. The private protected access level is not supported in versions of Java higher than 1.0; you should no longer be using it in your Java programs.

但是 Java 1.0 here or here.

的任何官方文档均未指定此功能

我的猜测是这个功能没有出现在官方 1.0 版本中,因为官方语言规范是 1996 年 8 月的,普林斯顿源最后一次修改 on February 1996

PS: Oracle 删除旧版本的存档真是可耻。

正如您在问题中提供的 link 表明 private protected 用于 class 的 element/member,当您希望 subclass能够访问该元素,但在其 package 中对其他 class 元素隐藏它。

Java 如果与 C++ 相比有一个额外的封装元素的概念——那就是 Package。当涉及到 privatepublicprotected.

等访问说明符时,人们还应该了解 Java 中包内或包外可以访问的内容

请注意,我已经解释了使用它的原因。当前版本当然没有

不,您不能同时使用 privateprotected。你的教程很奇怪。您所拥有的是所谓的包私有或 ot6 引用包保护访问。这是在未明确写入 acc6 限定符时启用的默认访问权限。

私有范围在现有 class 范围内。其中 Protected 可以在包内访问,class 由其他包中的 classes 扩展。

无缝地,如果你想让你的 variable/methods 在包外访问,你需要定义为 protected/public 否则私有或其他一些访问说明符。

受保护的方法通常可以从外部包和 sub-classes 中访问,即 class 必须扩展相应的 class 才能使用受保护的定义方法。

私有 methods/variables 在 class.They 范围内不能在 class 之外访问。

因此不能同时定义Private Protected!