4
this.addTreeSelectionListener(new TreeSelectionListener() {

        public void valueChanged(TreeSelectionEvent e) {

            // How do I access the parent tree from here?           
        }           
    });
4

2 に答える 2

36

使用できます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
    }
};
于 2009-11-05T09:25:42.217 に答える
3

TreeSelectionListenerはインターフェイスであるため、唯一の親クラスは でありObject、これを で呼び出すことができるはずですsuper

囲んでいるクラスのメソッドを呼び出す場合は、メソッド内で直接呼び出すことができます。

于 2009-11-05T09:22:43.830 に答える