Java의 Super Class of TreeSelectionListener에 어떻게 액세스합니까?
-
16-09-2019 - |
문제
this.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
// How do I access the parent tree from here?
}
});
해결책
TreeSelectionListener
인터페이스이므로 유일한 부모 클래스는 Object
, 당신은 전화 할 수 있어야합니다 super
.
Enclosing 클래스의 일부 방법을 호출한다면 방법 내에서 직접 호출 할 수 있습니다.
다른 팁
당신이 사용할 수있는 OuterClass.this
:
public class Test {
String name; // Would normally be private of course!
public static void main(String[] args) throws Exception {
Test t = new Test();
t.name = "Jon";
t.foo();
}
public void foo() {
Runnable r = new Runnable() {
public void run() {
Test t = Test.this;
System.out.println(t.name);
}
};
r.run();
}
}
그러나 액세스하기 만하면 a 회원 인스턴스 자체에 대한 참조를 얻지 않고 Enclosing 인스턴스에서 직접 액세스 할 수 있습니다.
Runnable r = new Runnable() {
public void run() {
System.out.println(name); // Access Test.this.name
}
};
제휴하지 않습니다 StackOverflow