¿Cómo accedo a la superclase de TreeSelectionListener en Java?
-
16-09-2019 - |
Pregunta
this.addTreeSelectionListener(new TreeSelectionListener() {
public void valueChanged(TreeSelectionEvent e) {
// How do I access the parent tree from here?
}
});
Solución
TreeSelectionListener
es una interfaz, por lo que sería Object
la única clase padre, que debe ser capaz de llamar con super
.
Si usted significó llamar a un método de la clase envolvente, se le puede llamar directamente como dentro de un método.
Otros consejos
Puede utilizar 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();
}
}
Sin embargo, si sólo necesita acceder a un miembro de en la instancia que encierra, en lugar de obtener una referencia a la propia instancia, sólo puede acceder a él directamente:
Runnable r = new Runnable() {
public void run() {
System.out.println(name); // Access Test.this.name
}
};
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow