반응형
Java 익명 클래스에서 "this"에 액세스
다음 코드가 주어진다 :
public interface Selectable {
public void select();
}
public class Container implements Selectable {
public void select() {
...
}
public void createAnonymousClass() {
Selectable s = new Selectable() {
public void select() {
//see comment below.
}
};
}
}
Container.select()
익명 클래스의 select()
메소드 에서 액세스하고 싶습니다 . 그러나 this.select()
다시 익명 클래스의 select()
메소드를 호출합니다 .
내 제안은 다음과 같습니다.
컨테이너에 필드를 소개합니다. 예 :
private Container self = this;
이제 익명 클래스 내에서 Container.select()
전화 self.select()
하여 액세스 할 수 있습니다 .
이것이 합리적인 방법입니까? 아니면 더 좋은 방법이 있습니까?
Container.this.select();
당신은 Container.this.select()
내부 클래스와 구별되도록 쓸 수 있습니다 !
참고 URL : https://stackoverflow.com/questions/1084112/access-this-from-java-anonymous-class
반응형
'Programing' 카테고리의 다른 글
double을 int로 변환 (0) | 2020.06.21 |
---|---|
JSP에서 HashMap을 반복하는 방법은 무엇입니까? (0) | 2020.06.21 |
Java에서 타이머를 설정하는 방법은 무엇입니까? (0) | 2020.06.21 |
Mac에서 sshfs로 마운트 된 디렉토리를 마운트 해제하십시오. (0) | 2020.06.20 |
파이썬에서 디스크에 기본 http 파일 다운로드 및 저장? (0) | 2020.06.20 |