![](/img/trans.png)
[英]How to access a private field of the super class of the super class with reflection in Java?
[英]How do I access the super class of TreeSelectionListener in Java?
this.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
// How do I access the parent tree from here?
}
});
你可以使用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();
}
}
但是,如果您只需要訪問封閉實例中的成員 ,而不是獲取對實例本身的引用,則可以直接訪問它:
Runnable r = new Runnable() {
public void run() {
System.out.println(name); // Access Test.this.name
}
};
TreeSelectionListener
是一個接口,因此唯一的父類是Object
,您應該可以使用super
調用它。
如果你的意思是調用封閉類的某個方法,你可以直接在方法中調用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.