Java:无法访问扩展子类中超类的受保护成员

我想对此进行一些讨论,但我无法推断出我的案例的答案。 仍然需要帮助。

这是我的代码:

package JustRandomPackage; public class YetAnotherClass{ protected int variable = 5; } 
 package FirstChapter; import JustRandomPackage.*; public class ATypeNameProgram extends YetAnotherClass{ public static void main(String[] args) { YetAnotherClass bill = new YetAnotherClass(); System.out.println(bill.variable); // error: YetAnotherClass.variable is not visible } } 

一些定义之后,上面的例子似乎令人困惑:

  1. Subclass is a class that extends another class. 2. Class members declared as protected can be accessed from the classes in the same package as well as classes in other packages that are subclasses of the declaring class. 

问题:为什么我不能从子类YetAnotherClass实例( bill对象)访问受保护的成员( int variable = 5 )?

作为声明类的子类的其他包中的类只能访问其自己的inheritanceprotected成员。

 public class ATypeNameProgram extends YetAnotherClass{ public ATypeNameProgram() { System.out.println(this.variable); // this.variable is visible } } 

…但不是其他对象inheritance的protected成员。

 public class ATypeNameProgram extends YetAnotherClass{ public ATypeNameProgram() { System.out.println(this.variable); // this.variable is visible } public boolean equals(ATypeNameProgram other) { return this.variable == other.variable; // error: YetAnotherClass.variable is not visible } } 

bill不是子类YetAnotherClass的一部分。 bill是一个单独的YetAnotherClass。

尝试int bill = this.variable; (在构造函数内)访问子类的成员。

如果YetAnotherClassATypeNameProgram位于同一个包中,则代码将起作用。 正如其他人所写,在其他情况下不会起作用。 这是工作示例。

 package my.example; public class MainClass extends MyAnotherClass { public static void main(String[] args) { MyAnotherClass bill = new MyAnotherClass(); System.out.println(bill.value); // this will work } } package my.example; public class MyAnotherClass { protected int value = 5; } 

当且仅当Bar可分配给FooFoo才能访问Bar类型的受保护实例成员。 即,如果我们可以写:

 Foo foo = new Bar(); 

例如,假设我们有:

 package a; public class Base { protected int protectedField; } 

然后我们可以这样:

 package b; import a.Base; public class Parent extends Base { void foo() { int i = this.protectedField; } void foo(Parent p) { int i = p.protectedField; } void foo(Child c) { int i = c.protectedField; } } class Child extends Parent { } 

这将编译,因为所有protectedField都是通过Parent实例访问的。 请注意,因为Parent引用可以是Child实例(即,我们可以编写Parent p = new Child(); ),所以我们可以访问c.protectedField

以下内容无法编译:

 package b; import a.Base; public class Parent extends Base { void foo(Stepchild sc) { int i = sc.protectedField; // ERROR } } class Stepchild extends Base {} 

因为Stepchild的实例不是Parent的实例。

有点令人困惑,这也不会编译:

 package b; import a.Base; public class Parent extends Base {} class Child extends Parent { void foo(Parent p) { p.protectedField; // ERROR } } 

这是因为Parent对象不是Child的超类或超接口,因此Child无法访问其受保护的成员。

如果您在记忆时遇到问题,只需考虑该类型是否可以写入类的类型的引用。 例如,我们可以写:

 Parent p = new Child(); 

但不能写

 Child c = new Parent(); // ERROR Parent p = new Stepchild(); // ERROR 

因此, Child无法访问Parent的受保护成员, Parent也无法访问Stepchild的受保护成员。

最后几点:

请记住, protected访问允许包之间的可见性。 根据我的经验,人们会忘记这一点。

最后, protected static成员始终在inheritance层次结构中可见。

您不是要创建扩展它的类的实例,而是创建父类的实例。 检查以下代码:

 public class ATypeNameProgram extends YetAnotherClass{ public static void main(String[] args) { YetAnotherClass bill = new YetAnotherClass(); System.out.println(bill.variable); // error: YetAnotherClass.variable is not visible ATypeNameProgram a = new ATypeNameProgram(); System.out.println(a.variable); //this will work } }