我想对此进行一些讨论,但我无法推断出我的案例的答案。仍然需要帮助。
这是我的代码:
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.
问题:为什么我不能int variable = 5
从子类YetAnotherClass
实例(bill
对象)访问受保护的成员()?