【发布时间】:2013-06-08 03:13:22
【问题描述】:
我想对此进行一些讨论,但我无法为我的案例推断出答案。仍然需要帮助。
这是我的代码:
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)?
【问题讨论】:
-
问题是什么?