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

查看:194
本文介绍了对匿名内部类使用最终的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 。我想知道是否一个。)这是常见的做法或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.

推荐答案

p>如果代码清晰可见,那就是这样,我不会说这样做是可怕的。

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天全站免登陆