public enum MessageType
{
MSG_ID_LOGIN_REQ(8501),
MSG_ID_LOGIN_RESP(8502),
MSG_ID_LOGOUT_REQ(8503),
MSG_ID_LOGOUT_RESP(8504),
MSG_ID_ROBOT_BASE_INFO(5501);
private int msgType;
private static Map<Integer, MessageType> mapping;
public int getMsgType() { return this.msgType; }
static {
mapping = new HashMap();
for (MessageType messageType : values()) {
if (mapping.put(Integer.valueOf(messageType.getMsgType()), messageType) != null)
throw new IllegalArgumentException("duplicated message type");
}
}
private MessageType(int msgType) {
this.msgType = msgType;
}
public static MessageType getInstance(int msgType) {
return (MessageType)mapping.get(Integer.valueOf(msgType));
}
}
通过静态的map对象,存储所有的values值,形成键值对,以后通过int类型获取直接从map中取,可以再生成一个key为name的map,直接获取对象