이 문제에 대한 토론이 필요하지만 제 경우에 대한 답을 추측 할 수 없습니다. 여전히 도움이 필요합니다. 여기 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
)에 액세스합니다.개체)?
무엇이 문제입니까? –