Java 是否有“私有保护”访问修饰符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41431533/
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
Does Java have a "private protected" access modifier?
提问by Andrew Li
I have seen some references refer to a access modifier in Java called private protected
(both words together):
我看到一些参考文献引用了 Java 中的访问修饰符,称为private protected
(两个词一起):
private protected someMethod() {
}
One of the pages I found referring to this is here. My school lesson also referred to this access modifier (and said it exists). Using it, however, results in an error in the Java language.
我找到的与此相关的页面之一是here。我的学校课也提到了这个访问修饰符(并说它存在)。但是,使用它会导致 Java 语言出错。
I tried with both variables and methods and I'm pretty sure it doesn't exist, but I want an explanation of what happened. Was it considered, then rejected? Or did it get removed in a newer version of Java?
我尝试了变量和方法,我很确定它不存在,但我想解释发生了什么。考虑过,然后拒绝?或者它是否在较新版本的 Java 中被删除?
Edit: I am not looking for info about the protected
keyword.
编辑:我不是在寻找有关protected
关键字的信息。
回答by Andrew Li
Removal of the access modifier
删除访问修饰符
Java did originally have the private protected
modifier, but it was removed in JDK 1.0.2 (the first stableversion, the Java 1.0 we know today). A few tutorials regarding JDK 1.0.2 (hereand here) say the following:
Java 最初确实有这个private protected
修饰符,但它在 JDK 1.0.2(第一个稳定版本,我们今天所知的 Java 1.0)中被删除了。一些关于 JDK 1.0.2 的教程(这里和这里)说明如下:
Note: The 1.0 release of the Java language supported five access levels: the four listed above plus
private protected
. Theprivate 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 版本支持五个访问级别:上面列出的四个加上
private protected
. 该private protected
访问级别不高的Java版本比1.0的支持; 您不应再在 Java 程序中使用它。
Another answer on SoftwareEngineering.SEstates:
SoftwareEngineering.SE 上的另一个答案指出:
Java originally had such a modifier. It was written
private protected
but removed in Java 1.0.
Java 原本就有这样的修饰符。它被编写
private protected
但在 Java 1.0 中被删除。
Now take a look at the Java Version History:
现在看看Java 版本历史:
JDK 1.0
The first version was released on January 23, 1996and called Oak. The first stable version, JDK 1.0.2, is called Java 1.
JDK 1.0
第一个版本于1996年1 月 23 日发布,名为 Oak。第一个稳定版本 JDK 1.0.2,称为 Java 1。
From this, we can conclude the tutorials regarding version 1.0.2 refer to the very first version, JDK 1.0, where the language was called Oak, but the one from SoftwareEngineering.SE refers to the first stable version, JDK 1.0.2 called Java 1.0, where it was removed.
由此,我们可以得出结论,关于 1.0.2 版的教程是指第一个版本 JDK 1.0,该语言被称为 Oak,但来自 SoftwareEngineering.SE 的教程指的是第一个稳定版本,JDK 1.0.2,称为 Java 1.0,它被删除。
Now if you try to search for it in the Java 1.0 documentation, you won't find it, because as mentioned earlier, it was removed in JDK 1.0.2, otherwise known as Java 1.0. This is proven again when you look at the "Last Modified" times for the link you posted. The link you posted was last modified in February of 1996. Java 1.0/JDK 1.0.2, when private protected
was removed, was released after February of 1996, and according to the specification, August of 1996.
现在,如果您尝试在Java 1.0 文档中搜索它,您将找不到它,因为如前所述,它已在 JDK 1.0.2(也称为 Java 1.0)中删除。当您查看您发布的链接的“上次修改时间”时,再次证明了这一点。您发布的链接最后一次修改是在 1996 年 2 月。Java 1.0/JDK 1.0.2 在private protected
被删除时是在 1996 年 2 月之后发布的,根据规范,1996 年 8 月。
Reason for removal
移除原因
Some sources also explain the reason for private protected
, such as thisone. To quote:
一些消息来源也解释了原因private protected
,例如这个。去引用:
What was private protected?
Early on, the Java language allowed for certain combinations of modifiers, one of which was
private protected
. The meaning ofprivate 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 theprivate protected
combination appeared at the same time. They patched some potential security holes, but confused many people.
什么是私人保护的?
早期,Java 语言允许某些修饰符组合,其中之一是
private protected
. 的含义private protected
是严格限制子类的可见性(并删除包访问权限)。这后来被认为有些不一致和过于复杂,不再支持。 [5][5]
protected
Java Beta2版本中修饰符的含义发生了变化,同时private protected
出现了组合。他们修补了一些潜在的安全漏洞,但让很多人感到困惑。
And the SoftwareEngineering.SE also supports this, by saying that it wasn't worth the inconsistencies and extra complexity, so it was removed early on.
而且 SoftwareEngineering.SE 也支持这一点,通过说它不值得不一致和额外的复杂性,所以它很早就被删除了。
Interpretation
解释
My interpretation of all this is that maybe, back in the Oak days, both were allowed to coexist (hence the combination). Since protected
's meaning had changed1, there may have been a need for allowing private
and protected
at the same time. The introduction became too complex and wasn't worth it, and was thus dropped in the end. By the time Java 1.0/JDK 1.0.2 rolled around, it had been dropped and thus cannot be found in the documentation.
我对这一切的解释是,在橡树时代,也许两者可以共存(因此是组合)。由于protected
的含义已更改1,因此可能需要同时允许private
和protected
。介绍变得太复杂,不值得,最终被放弃了。到 Java 1.0/JDK 1.0.2 推出时,它已被删除,因此无法在文档中找到。
1In the Oak Language Specification, Section 4.10, Access to Variables and Methods, it is noted that the default modifier was protected
:
1在Oak 语言规范的第 4.10 节,访问变量和方法中,注意到默认修饰符是protected
:
By default all variables and methods in a class are protected.
默认情况下,类中的所有变量和方法都是受保护的。
This is quite different from what we have today, the default package access. This may have paved the way for the need of private protected
, because private
was too restrictive and protected
was too lenient.
这与我们今天拥有的默认包访问有很大不同。这可能为 的需要铺平了道路private protected
,因为private
限制性太强且protected
太宽松了。
回答by m0skit0
There are confusing/unclear stories:
有一些令人困惑/不清楚的故事:
One, from the Princeton source you put, and also from MIT archives, states that:
一,来自您提供的普林斯顿来源,以及来自麻省理工学院的档案,指出:
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 版本支持五个访问级别:上面列出的四个加上私有保护。1.0 以上的 Java 版本不支持私有保护访问级别;您不应再在 Java 程序中使用它。
But this feature is not specified on any official documentation for Java 1.0 hereor here.
但是在此处或此处 的任何 Java 1.0 官方文档中均未指定此功能。
My guess is that this feature didn't make it to the official 1.0 version, since the official language specification is from August 1996 and Princeton source was last modified on February 1996.
我的猜测是这个特性没有进入官方 1.0 版本,因为官方语言规范是从 1996 年 8 月开始的,而普林斯顿源最后一次修改是在 1996 年 2 月。
PS: shame on Oracle for removing the archives for older versions.
PS:耻辱Oracle删除旧版本的档案。
回答by programmer_of_the_galaxies
As the link you provided in your question suggests private protected
was used on an element/member
of a class, when you want your subclass
to be able access the element but keep it hidden from other classes in its package
.
当你在你的问题中提供的链接建议private protected
使用上的element/member
一类,如果你希望你subclass
能够访问的元素,但保持它从其他类隐藏在它的package
。
Java
if compared to C++
has an extra concept of encapsulating elements - and that is a Package. One should also understand what is accessible within or outside a package in Java
when it comes to these access-specifiers like private
, public
& protected
.
Java
如果相比之下C++
有一个额外的封装元素的概念——那就是Package。Java
当涉及到这些访问说明符(如private
, public
& )时,人们还应该了解在包内或包外可以访问的内容protected
。
Please note that I have explained why it was used. Not in current version of course
请注意,我已经解释了使用它的原因。当然不在当前版本中
回答by AlexR
No, you can't use both private
a protected
together. Your tutorial is strange. What you do have is so called package private or in ot6 references package protected access. This is default access that is enabled when no acc6 qualifier is written explicitly.
不,你不能同时使用private
一个protected
在一起。你的教程很奇怪。您所拥有的是所谓的包私有或在 ot6 中引用包保护访问。这是在未明确写入 acc6 限定符时启用的默认访问。
回答by Tejas Gowda
Private scope is withing the existing class. Wherein Protected can be access within package and class extended by classes in other packages.
私有范围在现有类中。其中 Protected 可以在包内访问,也可以在其他包中的类扩展的类中访问。
Seamlessly if you want your variable/methods to be access outside the package you need to define as protected/public otherwise private or some other access specifiers.
无缝地,如果您希望在包外访问变量/方法,则需要将其定义为 protected/public 否则为 private 或其他一些访问说明符。
Protected methods are usually accessible from outside package and within sub-classes, i.e a class has to extend respective class to avail protected defined methods.
受保护的方法通常可以从包外部和子类内部访问,即类必须扩展各自的类才能使用受保护的定义方法。
Private methods/variables have scope within the class.They cant be accessible outside the class.
私有方法/变量在类内具有作用域。它们不能在类外访问。
Hence you can't define Private Protected at a same time!
因此您不能同时定义 Private Protected!