为匿名内部类使用最终的 1 元素数组 [英] Using final 1-element array for anonymous inner class

查看:25
本文介绍了为匿名内部类使用最终的 1 元素数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我偶然发现了从匿名内部类获取值到外部类中声明的变量的技巧.它有效,但感觉像是一个肮脏的黑客:

I stumbled across this trick for getting a value from an anonymous inner class to a variable which is declared in the outer class. It works, but it feels like a dirty hack:

private int showDialog()
{
    final int[] myValue = new int[1];

    JPanel panel = new JPanel();
    final JDialog dialog = new JDialog(mainWindow, "Hit the button", true);
    dialog.setDefaultCloseOperation( WindowConstants.DO_NOTHING_ON_CLOSE );

    JButton button = new JButton("Hit me!");
    button.addActionListener(new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            myValue[0] = 42;
            dialog.setVisible(false);
        }
    });

    panel.add(button);
    dialog.add(panel);
    dialog.pack();
    dialog.setVisible(true);

    return myValue[0];
}

(是的,我意识到这个例子可以用一个简单的 JOptionPane 代替,但我的实际对话框要复杂得多.)内部函数坚持它与之交互的所有变量都是 final,但我不能将 myValue 声明为 final,因为内部函数需要为其分配一个值.将它声明为一个 1 元素数组可以解决这个问题,但它似乎以某种方式可能是一件坏事TM.我想知道 a.) 这是常见做法还是 b.) 这样做可能会导致任何严重问题.

(Yes, I realize this example could be replaced with a simple JOptionPane, but my actual dialogs are much more complicated.) The inner function insists that all variables it interacts with be final, but I can't declare the myValue as final because the inner function needs to assign it a value. Declaring it as a 1-element array gets around this problem, but seems like it might be a Bad ThingTM somehow. I'm wondering if a.) this is common practice or b.) there's any serious problems that could result from doing this.

推荐答案

如果代码清晰易读,我不会说这样做很糟糕.

If the code is legible, which it is, I wouldn't say doing it that way is terrible.

另一种方法是让 JButton 调用具有 showDialog(允许)的类中的函数.该函数可以设置将返回的实例变量.但这对我来说似乎不太清晰,所以我实际上更喜欢你的方法.

An alternative is to have the JButton call a function in the class that has showDialog (which is allowed). The function could set an instance variable that will be returned. But that seems less legible to me, so I'd actually prefer your method.

除非您正在制作一个层次很深的 UI 框架,否则有时这些小技巧正是您应该做的事情.

Unless you are making a deeply hierarchical UI framework, sometimes these little hacks are exactly the sort of thing you should do.

如果你担心,你可以用私有内部类做基本相同的事情:

If you are concerned, you can do basically the same thing with a private inner class:

private class DialogReturnValue {
    public int value;
}

private int showDialog()
{
    final DialogReturnValue myValue = new DialogReturnValue();

    JPanel panel = new JPanel();
    final JDialog dialog = new JDialog(mainWindow, "Hit the button", true);
    dialog.setDefaultCloseOperation( WindowConstants.DO_NOTHING_ON_CLOSE );

    JButton button = new JButton("Hit me!");
    button.addActionListener(new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            myValue.value = 42;
            dialog.setVisible(false);
        }
    });

    panel.add(button);
    dialog.add(panel);
    dialog.pack();
    dialog.setVisible(true);

    return myValue.value;
}

还有 ActionListeners 可以查看(这很可能是正确"的方法).

And there's also ActionListeners to look at (which may well be the "right" approach).

这篇关于为匿名内部类使用最终的 1 元素数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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