[英]Java: cannot access a protected member of the superclass in the extending subclass
我想对此进行一些讨论,但我无法推断出我的案例的答案。 还是需要帮助。
这是我的代码:
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
)?
作为声明类的子类的其他包中的类只能访问它们自己继承的protected
成员。
package FirstChapter;
import JustRandomPackage.*;
public class ATypeNameProgram extends YetAnotherClass{
public ATypeNameProgram() {
System.out.println(this.variable); // this.variable is visible
}
}
...但不是其他对象继承的protected
成员。
package FirstChapter;
import JustRandomPackage.*;
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;
(在构造函数内)访问子类的成员。
如果YetAnotherClass
与ATypeNameProgram
位于同一个包中,您的代码将起作用。 正如其他人所写,它在其他情况下不起作用。 这是工作示例。
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
可分配给Foo
类Foo
只能访问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
成员在继承层次结构中始终可见。
您不是创建扩展它的类的实例,而是创建父类的实例。 检查下面的代码:
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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.