我正在使用如下所示的(旧版,不可更改)枚举:
enum MsgEnum {
private int number;
private int msg;
MSG_OK("OK", 0);
MSG_NOK("NOK, 1);
//lots of other messages
private MsgEnum(String msg, int number) {
this.msg = msg;
this.number = number;
}
}
如果我有number
,我怎样才能最好地找出哪个MsgEnum
与数字匹配(假设它们是唯一的)?
除了以下还有什么更好的方法吗?
class MyEnumHelper() {
public static MsgEnum MsgEnumfromNumber(int number) {
for (MsgEnum m : MsgEnum.values()) {
if (m.getNumber() == number) {
return m;
}
}
return null;
}
}