简体   繁体   English

受保护的内部类不能从另一个包的子类访问

[英]Protected inner-class is NOT accessible from subclass of another package

I have the following code in Java: 我在Java中有以下代码:

package a;
public classs ClassInOtherPackage{
    protected void protectedMethod(){}
     protected class protectedInnerClass {}
}

package b;
import a.*;
public class MyClass extends ClassInOtherPackage{
  public MyClass(){
    MyClass x = new MyClass();
    x.protectedMethod();  //<-- This one ok
    //UPDATED: Declaration is ok
    MyClass.protectedInnerClass y; //<-- This one ok
    //UPDATED: Only when instantiated is not allowed, why?
    y = x.new protectedInnerClass(); //<-- Not allow when instantiated.
  }
}

Refer to the Java documentation: 请参阅Java文档:

"The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package." “protected修饰符指定只能在自己的包中访问该成员(与package-private一样),此外,还可以在另一个包中通过其类的子类访问。”

Why can't I instantiate the inner protected class as shown above? 为什么我不能实例化如上所示的内部受保护类?

In JLS 8.8.9 JLS 8.8.9中

8.8.9. 8.8.9。 Default Constructor 默认构造函数

... if the class is declared protected , then the default constructor is implicitly given the access modifier protected ( §6.6 ); ...如果该类被声明为protected ,则默认构造函数被隐式赋予访问修饰符protected§6.6 ); ... ...

So the implicitly declared constructor is: 所以隐式声明的构造函数是:

protected class protectedInnerClass {
    protected protectedInnerClass(){
        super();
    }
}

You code won't compile because the constructer is inaccessible. 您的代码将无法编译,因为构造函数不可访问。

The default constructor of protectedInnerClass is protected , not the class . protectedInnerClass的默认构造函数是protectedInnerClass protected ,而不是类。 You need to define a public constructor to your inner class : 您需要为内部类定义一个public构造函数:

protected class protectedInnerClass {

  public protectedInnerClass () {}

}

MyClass can access the protected members of ClassInOtherPackage , but it cannot access the protected members of protectedInnerClass , since its constructor is protected hence you get such a compilation error. MyClass可以访问ClassInOtherPackageprotected成员,但它无法访问protectedprotectedInnerClass protected成员,因为它的构造函数protected因此会出现这样的编译错误。

As johnchen902 said the default constructor of a protected class is implicitly protected. 正如johnchen902所说,受保护类的默认构造函数是隐式保护的。 Protected means that only subclasses and classes within the same package can access it. 受保护意味着只有同一个包中的子类和类才能访问它。

  1. The protectedInnerClass doesn't declare a constructor thus it is implicitly protected. protectedInnerClass不声明构造函数,因此它是隐式保护的。
  2. MyClass is not a subclass of protectedInnerClass, thus it can not access it. MyClass不是protectedInnerClass的子类,因此无法访问它。

So you have to make it public or you can modify your code like this: 所以你必须公开它,或者你可以像这样修改你的代码:

public class MyClass extends ClassInOtherPackage {

    public MyClass() {
        MyClass x = new MyClass();
        x.protectedMethod(); // <-- This one ok
        MyClass.protectedInnerClass y = x.new MyProtectedInnerClass();
    }

    protected class MyProtectedInnerClass extends protectedInnerClass {

    }
}

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

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