从内部匿名类访问外部匿名类 [英] Access outer anonymous class from inner anonymous class

查看:126
本文介绍了从内部匿名类访问外部匿名类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是好奇。有没有办法访问另一个匿名类中的匿名类中的父级?

I'm just curious. Is there a way to access parent in anonymous class that is inside another anonymous class?

我让这个例子创建一个 JTable 子类(匿名类)覆盖 changeSelection 并在里面创建另一个匿名类。

I make this example create a JTable subclass (anonymous class) override changeSelection and inside i create another anonymous class.

MCVE:

public class Test{

    public static void main(String args []){

        JTable table = new JTable(){

            @Override
            public void changeSelection(
                final int row, final int column,
                final boolean toggle, final boolean extend) {

                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        super.changeSelection(row, column, toggle, extend); 
                        //more code here
                    }
                });
            }
        };

    }//end main

}//end test 

我如何引用 super.changeSelection(..)

推荐答案

不幸的是,你必须给外部匿名类命名:

Unfortunately, you will have to give a name to the outer anonymous class:

public class Test{

    public static void main(String args []){

        class Foo extends JTable {

            @Override
            public void changeSelection(
                final int row, final int column,
                final boolean toggle, final boolean extend) {

                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        Foo.super.changeSelection(row, column, toggle, extend); 
                        //more code here
                    }
                });
            }
        };

        JTable table = new Foo();

    }//end main

}//end test 

这篇关于从内部匿名类访问外部匿名类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆