简体   繁体   English

静态嵌套类可以访问外部类的私有构造函数

[英]Static nested class has access to private constructor of outer class

It is stated that: 指出

A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. 静态嵌套类与它的外部类(和其他类)的实例成员进行交互,就像其他任何顶级类一样。 In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience. 实际上,静态嵌套类在行为上是顶级类,为了包装方便,该顶级类已嵌套在另一个顶级类中。

So how to explain this static nested class has access to private constructor of its enclosing class? 那么,如何解释这个静态嵌套类可以访问其封闭类的私有构造函数呢?

So how to explain this static nested class has access to private constructor of its enclosing class? 那么,如何解释这个静态嵌套类可以访问其封闭类的私有构造函数呢?

Because the scope and accessibility of any private member (field, constructor or method) is the complete body of its declaring class, including any nested classes. 因为任何private成员(字段,构造函数或方法)的范围和可访问性都是其声明类(包括任何嵌套类)的完整主体。

From the Java Language Specification section 6.6.1 : Java语言规范的第6.6.1节中

if the member or constructor is declared private , then access is permitted if and only if it occurs within the body of the top level class (§7.6) that encloses the declaration of the member or constructor. 如果将成员或构造函数声明为private ,则仅当访问发生在封装成员或构造函数的声明的顶级类(第7.6节)的主体内时,才允许访问。

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

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