-3
설명서에 "InnerClass의 인스턴스는 OuterClass 인스턴스 내에 만 존재하고 그 인스턴스를 포함하는 메서드 및 필드에 직접 액세스 할 수 있습니다."라는 문구가 있습니다. 즉, 내부 클래스의 인스턴스를 사용하면 외부 클래스의 멤버에 액세스 할 수 있습니다. 그러나 나는 그렇게 할 수 없다.내부 클래스 '인스턴스가 외부 클래스'데이터 멤버에 액세스 할 수 없습니다.
public class TopLevel {
private int length;
private int breadth;
public class NonstaticNested{
private static final int var1 = 2;
public int nonStaticNestedMethod(){
System.out.println(var1);
length = 2;
breadth = 2;
return length * breadth;
}
}
public static void main(String[] args) {
TopLevel topLevel = new TopLevel();
NonstaticNested nonStaticNested = topLevel.new NonstaticNested();
// Trying to access the length variable on 'nonStaticNested' instance, but not able to do so.
}
}
글쎄 ... 당신이하고있는 일이나 단서가없는 것. (코드 없음) – SomeJavaGuy
@SomeJavaGuy가 방금 코드를 추가했습니다. 도와주세요. –
서식을 수정하십시오. 당신이하고있는 것을보기가 어렵습니다. –