java中对常量数据的配置可以使用枚举类型实现,它也有对象、属性、方法等,枚举类型将它的对象(实例)设置为常量方便读取和使用。
public enum MyFamilyMember {
SONGWENLONG("虫子哥","老公"),
QIFAN("臭亓帆","臭老婆");
private String name;
private String description;
MyFamilyMember(){}
MyFamilyMember(String name, String description) {
this.name = name;
this.description = description;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public static void main(String[] args) {
System.out.println(MyFamilyMember.QIFAN.name+":我是"+MyFamilyMember.QIFAN.description);
}
}
三、枚举可实现接口:
举一个项目使用的返回值包装类;首先 枚举类ResultCode 实现接口IResultCode ;返回值包装类中引用枚举类ResultCode
接口:
public interface IResultCode extends Serializable {
String getMessage();
int getCode();
}
enum类:
public enum ResultCode implements IResultCode {
SUCCESS(200, "操作成功"),
FAILURE(500, "业务异常"),
UN_AUTHORIZED(401, "请求未授权"),
CLIENT_UN_AUTHORIZED(401, "客户端请求未授权"),
NOT_FOUND(404, "404 没找到请求"),
MSG_NOT_READABLE(500, "消息不能读取"),
METHOD_NOT_SUPPORTED(405, "不支持当前请求方法"),
MEDIA_TYPE_NOT_SUPPORTED(415, "不支持当前媒体类型"),
REQ_REJECT(403, "请求被拒绝"),
INTERNAL_SERVER_ERROR(500, "服务器异常"),
PARAM_MISS(500, "缺少必要的请求参数"),
PARAM_TYPE_ERROR(500, "请求参数类型错误"),
PARAM_BIND_ERROR(500, "请求参数绑定错误"),
PARAM_VALID_ERROR(500, "参数校验失败");
final int code;
final String message;
public int getCode() {
return this.code;
}
public String getMessage() {
return this.message;
}
private ResultCode(int code, String message) {
this.code = code;
this.message = message;
}
}
R类:
public class R三、枚举的比较:implements Serializable { private static final long serialVersionUID = 1L; //"状态码" private int code; //"是否成功", private boolean success; //"承载数据" private T data; //"返回消息" private String msg; private R(IResultCode resultCode) { this(resultCode, (Object)null, resultCode.getMessage()); } private R(IResultCode resultCode, String msg) { this(resultCode, (Object)null, msg); } private R(IResultCode resultCode, T data) { this(resultCode, data, resultCode.getMessage()); } private R(IResultCode resultCode, T data, String msg) { this(resultCode.getCode(), data, msg); } private R(int code, T data, String msg) { this.code = code; this.data = data; this.msg = msg; this.success = ResultCode.SUCCESS.code == code; } public static boolean isSuccess(@Nullable R> result) { return (Boolean)Optional.ofNullable(result).map((x) -> { return ObjectUtil.nullSafeEquals(ResultCode.SUCCESS.code, x.code); }).orElse(Boolean.FALSE); } public static boolean isNotSuccess(@Nullable R> result) { return !isSuccess(result); } public static R data(T data) { return data(data, "操作成功"); } public static R data(T data, String msg) { return data(200, data, msg); } public static R data(int code, T data, String msg) { return new R(code, data, data == null ? "暂无承载数据" : msg); } public static R success(String msg) { return new R(ResultCode.SUCCESS, msg); } public static R success(IResultCode resultCode) { return new R(resultCode); } public static R success(IResultCode resultCode, String msg) { return new R(resultCode, msg); } public static R fail(String msg) { return new R(ResultCode.FAILURE, msg); } public static R fail(int code, String msg) { return new R(code, (Object)null, msg); } public static R fail(IResultCode resultCode) { return new R(resultCode); } public static R fail(IResultCode resultCode, String msg) { return new R(resultCode, msg); } public static R status(boolean flag) { return flag ? success("操作成功") : fail("操作失败"); } public int getCode() { return this.code; } public boolean isSuccess() { return this.success; } public T getData() { return this.data; } public String getMsg() { return this.msg; } public void setCode(int code) { this.code = code; } public void setSuccess(boolean success) { this.success = success; } public void setData(T data) { this.data = data; } public void setMsg(String msg) { this.msg = msg; } public String toString() { return "R(code=" + this.getCode() + ", success=" + this.isSuccess() + ", data=" + this.getData() + ", msg=" + this.getMsg() + ")"; } public R() { } }
枚举都是常量,所以用==比较即可;



