どのように私はJavaで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
されるだろう。
は、メソッド内として直接呼び出すことができます。
他のヒント
あなたは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
}
};
所属していません StackOverflow