Java枚举类型如何使用
程序员文章站
2022-06-14 22:08:09
将写死的数据或者参数写入枚举类中,需要时直接调用。/** * @description: 状态值枚举类 * @author: Aweicy * @create: 2020-09-29 21:30 **/public enum StatusEnum { STATUS_101(101,"错误码101"), STATUS_DEFAULT(-1,""); private int code; private String msg; public static...
将写死的数据或者参数写入枚举类中,需要时直接调用。
/**
* @description: 状态值枚举类
* @author: Aweicy
* @create: 2020-09-29 21:30
**/ public enum StatusEnum { STATUS_101(101,"错误码101"), STATUS_DEFAULT(-1,""); private int code; private String msg; public static StatusEnum getCode(int code){ for (StatusEnum e: StatusEnum.values()) { if(e.code == code){ return e; } } return STATUS_DEFAULT; } StatusEnum(int code, String msg) { this.code = code; this.msg = msg; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }
测试
public static void main(String[] args) { String msg = StatusEnum.getCode(101).getMsg(); System.out.println(msg); }
本文地址:https://blog.csdn.net/qq_35970562/article/details/108876425