繁体   English   中英

如何在Java中访问TreeSelectionListener的超类?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM