我想對此進行一些討論,但我無法推斷出我的案例的答案。仍然需要幫助。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
實例()的受保護成員(int variable = 5
)對象)?
問題是什麼? –