定义一个java类,在类中定义多个枚举
/**
* 常量类(枚举)
*
* @author GaoJinShan
*/
public class Constants {
/**
* 接口状态码
*
* @author GaoJinShan
*/
public enum ResultCode {
UNKNOWN_ERROR("-1", "未知错误"),
SUCCESS("200", "成功"),
PARAM_ERROR("201", "参数不合法"),
DATABASE_ERROR("202", "数据库异常"),
EXIST("403", "已存在");
private String code;
private String message;
ResultCode(String code, String message) {
this.code = code;
this.message = message;
}
public String getCode() {
return code;
}
public String getMessage() {
return message;
}
}
/**
* 网络类型
*
* @author GaoJinShan
*/
public enum NetworkType {
WIRED(1, "有线网"), WIRELESS(2, "无线网");
private final Integer value;
private final String name;
NetworkType(Integer value, String name) {
this.value = value;
this.name = name;
}
public Integer getValue() {
return value;
}
public String getName() {
return name;
}
}
}
注: 这样做解决了常量多且无序的痛点
调用
/**
* @param @param object
* @param @return 设定文件
* @return Result 返回类型
* @Title: success
* @Description: 成功有返回
*/
public static Result success(Object object) {
Result result = new Result(Constants.ResultCode.SUCCESS.getCode(), Constants.ResultCode.SUCCESS.getMessage());
result.setData(object);
return result;
}