枚举的Java自定义枚举值

时间:2023-01-26 14:48:03

I have enum like this

我有这样的枚举

public enum Sizes {
    Normal(232), Large(455);

    private final int _value;

    Sizes(int value) {
        _value = value;
    }

    public int Value() {
        return _value;
    }
}

Now I can call Sizes.Normal.Value() to get integer value, but how do I convert integer value back to enum?

现在我可以调用Sizes.Normal.Value()来获取整数值,但是如何将整数值转换回枚举?

What I do now is:

我现在做的是:

public Sizes ToSize(int value) {
    for (Sizes size : Sizes.values()) {
        if (size.Value() == value)
            return size;
    }
    return null;
}

But that's only way to do that? That's how Java works?

但这是唯一的方法呢?这就是Java的工作原理?

2 个解决方案

#1


8  

Yes that's how it's done, generally by adding a static method to the enum. Think about it; you could have 10 fields on the enum. Do you expect Java to set up lookups for all of them?

是的,它是如何完成的,通常是通过向枚举添加静态方法。想一想;你可以在枚举上有10个字段。您是否希望Java为所有这些设置查找?

The point here is that Java enums don't have a 'value'. They have identity and an ordinal, plus whatever you add for yourself. This is just different from C#, which follows C++ in having an arbitrary integer value instead.

这里的要点是Java枚举没有'值'。他们有身份和序数,加上你为自己添加的任何东西。这与C#不同,C#在C ++之后具有任意整数值。

#2


1  

It's been a while since I last worked with Java, but if I recall correctly, there's no relation between your _value field and the enum's ordinal. AFAIK you could have two entries with the same _value, for instance. As @bmargulies pointed out, you could have many fields in the enum, and nothing constrain you to use distinct values for each (or all) of them.

自从我上次使用Java以来​​已经有一段时间了,但是如果我没记错的话,你的_value字段和enum的序数之间没有任何关系。例如,AFAIK你可以有两个具有相同_value的条目。正如@bmargulies指出的那样,你可以在枚举中有很多字段,并且没有任何限制你为每个(或全部)使用不同的值。

See also this related question. Apparently, you can't directly set the ordinal of your entries.

另请参阅此相关问题。显然,您无法直接设置条目的序数。

#1


8  

Yes that's how it's done, generally by adding a static method to the enum. Think about it; you could have 10 fields on the enum. Do you expect Java to set up lookups for all of them?

是的,它是如何完成的,通常是通过向枚举添加静态方法。想一想;你可以在枚举上有10个字段。您是否希望Java为所有这些设置查找?

The point here is that Java enums don't have a 'value'. They have identity and an ordinal, plus whatever you add for yourself. This is just different from C#, which follows C++ in having an arbitrary integer value instead.

这里的要点是Java枚举没有'值'。他们有身份和序数,加上你为自己添加的任何东西。这与C#不同,C#在C ++之后具有任意整数值。

#2


1  

It's been a while since I last worked with Java, but if I recall correctly, there's no relation between your _value field and the enum's ordinal. AFAIK you could have two entries with the same _value, for instance. As @bmargulies pointed out, you could have many fields in the enum, and nothing constrain you to use distinct values for each (or all) of them.

自从我上次使用Java以来​​已经有一段时间了,但是如果我没记错的话,你的_value字段和enum的序数之间没有任何关系。例如,AFAIK你可以有两个具有相同_value的条目。正如@bmargulies指出的那样,你可以在枚举中有很多字段,并且没有任何限制你为每个(或全部)使用不同的值。

See also this related question. Apparently, you can't directly set the ordinal of your entries.

另请参阅此相关问题。显然,您无法直接设置条目的序数。