[英]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?
}
});
You can use OuterClass.this
: 你可以使用
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();
}
}
However, if you just need to access a member in the enclosing instance, rather than getting a reference to the instance itself, you can just access it directly: 但是,如果您只需要访问封闭实例中的成员 ,而不是获取对实例本身的引用,则可以直接访问它:
Runnable r = new Runnable() {
public void run() {
System.out.println(name); // Access Test.this.name
}
};
TreeSelectionListener
is an interface, so the only parent class would be Object
, which you should be able to call with super
. TreeSelectionListener
是一个接口,因此唯一的父类是Object
,您应该可以使用super
调用它。
If you meant calling some method of the enclosing class, you can call it directly as within a method. 如果你的意思是调用封闭类的某个方法,你可以直接在方法中调用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.