用了四年的.net,今年转做java,内心一直吐槽java的烦琐,今天发现了一个java值得我为之点赞的地方
java的枚举居然可以这么玩,废话不多,上demo
package com.sunline.sms.common; public enum ErrorCode { e0000 { @Override public String getCode() { return "0000"; } @Override public String getDesc() { return "交易成功"; } }, e0001 { @Override public String getCode() { return "0001"; } @Override public String getDesc() { return "参数为空"; } }, e0002 { @Override public String getCode() { return "0002"; } @Override public String getDesc() { return "请重新获取短信"; } }, e0003 { @Override public String getCode() { return "0003"; } @Override public String getDesc() { return "短信已失效"; } }, e0004 { @Override public String getCode() { return "0004"; } @Override public String getDesc() { return "验证码超过错误次数,请重新获取"; } }, e0005 { @Override public String getCode() { return "0005"; } @Override public String getDesc() { return "验证码错误,请重新输入"; } }, e9000 { @Override public String getCode() { return "9000"; } @Override public String getDesc() { return "获取验证码失败"; } } ; public abstract String getDesc(); public abstract String getCode(); }
使用时如下:
throw new ProcessException(ErrorCode.e0001.getCode(),ErrorCode.e0001.getDesc());