我需要通过 int在枚举中查找。枚举如下:
public enum ErrorCode{
MissingReturn(1,"Some long String here"),
InvalidArgument(2,"Another long String here");
private final int shortCode ;
private final String detailMessage;
ErrorCode(shortCode ,detailMessage){
this.shortCode = shortCode ;
this.detailMessage= detailMessage;
}
public String getDetailedMessage(){
return this.detailMessage;
}
public int getShortCode(){
return this.shortCode ;
}
}
现在需要有一个查找方法,它需要一个int
代码,并且应该返回String
与存储在Enum
.Passing 中的代码有关的消息。传递“1”应该返回字符串“Some long String here”。实现此功能的最佳方法是什么?
public static String lookUpMessageFromCode(int code){
}
PS:这个类 EnumMap
对这种用例有用吗?如果是,请告诉我为什么?