枚举的简单应用

枚举,可以用来表示状态。包括返回类型状态、错误类型状态等等。
枚举,可以简单理解成是几个常量加在对象类里面。
枚举是用来构建常量数据结构的模板,模块可扩展。枚举的使用增强了程序的健壮性,比如在引用一个不存在的枚举值的时候,编译器会报错。
枚举规定构造方法必须为private修饰符所修饰,将枚举类型中的构造方法设置为private,防止调用方代码实例化一个枚举对象。

直接使用枚举常量

ErrorType.java如下:

   public enum ErrorType {
		SUCCESS("0000", "成功"), 
		SESSION("0001", "session失效"),
		PERMSION("0002","无权限"),
		TOKEN("0003","token失效"),
		FAIL("1111", "失败");
	
	public String code;
	public String msg;
	
	private ErrorType(String code, String msg) {
		this.code = code;
		this.msg = msg;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}
	
  }

接下来就可以在另一个类中使用定义好的枚举变量了,如下所示:

	public JSONObject getErrorType() {
		JSONObject returnObject = new JSONObject();
		returnObject.put("errCode", ErrorType.SUCCESS.getCode());
		returnObject.put("description", ErrorType.SUCCESS.getMsg());
		return returnObject;
	}

使用枚举去掉if else代码

我们经常会看到以下这样的代码:

    /**
     * 不建议使用这样的写法。
     * 这种存在大量ifelse的代码,可以使用枚举去处理。
     *
     * @param type
     * @return
     */
    public static String getStatus(String type) {
        String status;
        if ("1".equals(type)) {
            status="成功";
        } else if ("2".equals(type)) {
            status = "失败";
        } else if ("3".equals(type)) {
            status = "等待";
        } else {
            status="";
        }
        return status;
    }

大量的if else代码,非常臃肿。
当各种类型对应各种状态时,简单的可以直接使用Map处理。
最好的还是使用枚举,因为枚举的作用范围比Map广,而且枚举常量的参数可以有多个。
通过遍历枚举常量,可以去掉大量的if else代码。主要通过Enum的values()方法实现。

枚举,如下:

public enum StatusEnum {
    Success("1", "成功"),
    Failure("2", "失败"),
    Wait("3", "等待"),

    ;

    private String status;
    private String type;

    StatusEnum(String type, String status) {
        this.status = status;
        this.type = type;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    /**
     * 遍历枚举常量,代替大量的if,else代码。
     *
     * @param type
     * @return
     */
    public static String getStatusByValues(String type) {
        for (StatusEnum statusEnum : StatusEnum.values()) {
            if (statusEnum.getType().equals(type)) {
                return statusEnum.getStatus();
            }
        }
        return "";
    }
}

运用如下:

public class StatusEnumDemo {
    public static void main(String[] args) {
        String type="1";
        String status= StatusEnum.getStatusByValues(type);
        System.out.println(String.format("类型%s对应的状态为%s",type,status));
    }
}

可以看到,通过使用枚举,去掉了大量的if else代码,非常实用。

posted on   乐之者v  阅读(199)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示