Java枚举类
今天写代码时候,上司告诉我type没有自己的解析类,调类的懒人慌了,我毕竟是个菜鸡,于是看了下前人的代码,仿写了一个枚举类,可以传入需要解析的代码,传出需要的参数
附上代码
package **.**;
import cn.web.util.JSONUtil;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonFormat;
/**Type解析类
* Created by zhanchi 2016/9/27.
* 记得最后getTypeName()
*/
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum MType {
//每个元素对应下面的this
ANY(0, "","不限",99),
UNKNOWN(9,"","未知", 99),
TYPE1(1,"","标签1", 1),
TYPE2(2,"","标签2", 2),
TYPE3(3,"","标签3", 3);
/**标签id*/
private int typeId;
/**标签代码*/
private String typeCode;
/**标签名字*/
private String typeName;
/**顺序*/
private int sequence;
private MType(int typeId,String typeCode, String typeName, int sequence) {
this.typeId = typeId;
this.setTypeCode(typeCode);
this.typeName = typeName;
this.sequence = sequence;
}
/** 通过typeId获取MType对象*/
public static MType getMType(int typeId) {
for(MType e : MType.values()) {
if(e.getTypeId() == typeId) {
return e;
}
}
return getMType(999999);
}
/** 通过typeCode获取MType对象*/
public static MType getTypeCode(String typeCode) {
for(MType e : MType.values()) {
if(typeCode.equals(e.getTypeCode())) {
return e;
}
}
return getTypeCode(String.valueOf(999999));
}
public String toString() {
return JSONUtil.objectToJsonStr(this);
}
public int getTypeId() {
return typeId;
}
public void setTypeId(int typeId) {
this.typeId = typeId;
}
public String getTypeName() {
return typeName;
}
public void setTypeName(String typeName) {
this.typeName = typeName;
}
public int getSequence() {
return sequence;
}
public void setSequence(int sequence) {
this.sequence = sequence;
}
public String getTypeCode() {
return typeCode;
}
public void setTypeCode(String typeCode ) {
this.typeCode = typeCode ;
}
}