简体   繁体   English

使用可变对象作为常量

[英]Using mutable objects as a constant

Adding final to a primitive type variable makes it immutable . final添加到基本类型变量使其不可变
Adding final to an object prevents you from changing its reference , but is still mutable . final添加到对象会阻止您更改其引用 ,但仍然是可变的

I'm trying to use ImageIcon as a final constant, but at the same time, I don't want the image to be changeable. 我正在尝试使用ImageIcon作为最终常量,但与此同时,我不希望图像可以更改。 Is it okay to just use mutable objects as a constant, or are there any alternatives? 是否可以将可变对象用作常量,还是有其他选择?

Example Constants: 示例常量:

public static final ImageIcon bit0 = new ImageIcon("bit0.png");
public static final ImageIcon bit1 = new ImageIcon("bit1.png");

Using the Constants: 使用常量:

JButton button = new JButton(ClassName.bit0);
JLabel label = new JLabel(ClassName.bit1);

I'm trying to use ImageIcon as a final constant, but at the same time, I don't want the image to be changeable. 我正在尝试使用ImageIcon作为最终常量,但与此同时,我不希望图像可以更改。 Is it okay to just use mutable objects as a constant, or are there any alternatives? 是否可以将可变对象用作常量,还是有其他选择?

Whether it's okay is up to you. 是否可以取决于你。 :-) Exposing a public reference to a mutable object leaves it open to mutation, as you said, so probably not ideal. :-)如你所说,暴露一个可变对象的公共引用使它可以突变,所以可能不理想。

In terms of alternatives: Since the JButton constructor you're using accepts Icon , and Icon is quite a simple interface with no mutation methods, you might want to create yourself an ImmutableIcon wrapper class and expose that. 在替代方面:由于您使用的JButton构造函数接受Icon ,而Icon是一个非常简单的接口,没有变异方法,您可能想要自己创建一个ImmutableIcon包装类并公开它。

Eg: 例如:

class ImmutableIcon implements Icon {
    private Icon icon;

    public ImmutableIcon(Icon i) {
        this.icon = i;
    }

    public int getIconHeight() {
        return this.icon.getIconHeight();
    }

    public int getIconWidth() {
        return this.icon.getIconWidth();
    }

    public void paintIcon(Component c, Graphics g, int x, int y) {
        this.icon.paintIcon(c, g, x, y);
    }
}

then 然后

public static final Icon bit0 = new ImmutableIcon(new ImageIcon("bit0.png"));

Because ImageIcon is a reference type (instead of primitive), final here only means that the reference will always point to the same object. 因为ImageIcon是引用类型(而不是基本类型),所以final在这里仅表示引用将始终指向同一个对象。 It says nothing about the state of that object. 它没有说明该对象的状态。

If you're concerned about someone changing the object's state, you can wrap it in another object whose interface you have control over, or simply upcast to Icon if your other uses of the icon allow for that, eg 如果您担心某人更改了对象的状态,您可以将其包装在您可以控制其界面的另一个对象中,或者如果您对该图标的其他用途允许,则只需将其更新为Icon ,例如

public static final Icon bit0 = new ImageIcon("bit0.png");

I'd also reconsider whether you need to expose the icon variable as a class constant. 我还要重新考虑是否需要将icon变量公开为类常量。 If you only ever make one instance of the containing class, then there are no savings from reuse. 如果你只创建一个包含类的实例,那么重用就没有节省。

As for whether or not mutable class constants are okay in general, I don't think there's any convention discouraging it, it just isn't always that useful. 至于可变类常量是否一般是正常的,我认为没有任何惯例阻止它,它并不总是那么有用。 And since they're not garbage collected, you must be careful not to leak memory. 而且由于它们不是垃圾收集,你必须小心不要泄漏内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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