use of enumeration types

 

The enumeration type is to call the constructor, pass the value to the constructor, the constructor assigns the enumeration value to the property, and then get it.

The way to call the constructor is not the traditional new, but directly click into the enumeration type, and the enumeration type automatically calls the constructor

 

 

 

 

 

ImgResp imgResp= new ImgResp();

Imginfo imginfo = new Imginfo();

imginfo.setImgUrl("bluckname="+imgInfoReq.getBluckname()+"&fName="+fileNameForm);

imginfo.setFileNameForm(fileNameForm);

imgResp.setResult(imginfo);

imgResp.setReturnCode(WeixinErrorMsg.SUCCESS.getCode());

imgResp.setReturnMsg(WeixinErrorMsg.SUCCESS.getText());

 

 

 

 

 

 

 

 

 

package com.itm.weixin.common;

 

import org.apache.commons.lang.StringUtils;

 

public enum WeixinErrorMsg {

 

SUCCESS("A0001","成功"),

 

PARAMETER_WRONG("A0001","The parameter is invalid");

 

WeixinErrorMsg(String code,String text){

this.code = code;

this.text = text;

}

 

private String code;

 

private String text;

 

public String getCode() {

return code;

}

 

public void setCode(String code) {

this.code = code;

}

 

public String getText() {

return text;

}

 

public void setText(String text) {

this.text = text;

}

 

 

public static WeixinErrorMsg getByCode(String code){

if(StringUtils.isEmpty(code))

return null;

WeixinErrorMsg [] options = WeixinErrorMsg.values();

for (int i = 0; i < options.length; i++) {

if(options[i].getCode().equals(code)){

return options[i];

}

}

return null;

}

 

public static String getTextByCode(String code){

WeixinErrorMsg option = getByCode(code);

if(option == null)

return null;

return option.getText();

}

}

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326189545&siteId=291194637