enum类自定义属性

这就是enum比static静态变量好用的地方了,可以赋予每一个枚举值若干个属性,例如

实例1:

public enum GasStationChannel {
ZH("中化", "100001"),
APP("APP", "100002"),
QZ("撬装", "100003"),
ZYW("找油网", "100004"),
YZG("油掌柜", "100005"),
YZX("油战线", "100006"),
SHELL("壳牌", "100007"),
CHEBEI("车呗", "100008"),
SHANGAO("山东高速", "100009"),
GUANDE("冠德", "100010"); private String name;
private String code; GasStationChannel(String name, String code) {
this.name = name;
this.code = code;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getCode() {
return code;
} public void setCode(String code) {
this.code = code;
} public static GasStationChannel parse(String code) {
if(code==null){
return null;
}
for (GasStationChannel channelType : GasStationChannel.values()) {
if (channelType.getCode().equals(code)) {
return channelType;
}
}
return null;
} @Override
public String toString() {
return "name:"+this.name+",code:"+this.code;
}}

 实例2:

public enum Domain {

    XB("11","西北"),
HD("13","华东"),
DB("14","东北"),
HB("15","华北"); private String code;
private String name; Domain(String code,String name) {
this.code = code;
this.name = name;
} public String getCode() {
return code;
}
public String getName(){
return name;
} /**
* 根据domain code,返回枚举类型
*/
public static Domain getDomain(String code) throws Exception { Domain domain = null;
switch (code.trim()) {
case "11":
domain = XB;
break;
case "13":
domain = HD;
break;
case "14":
domain = DB;
break;
case "15":
domain = HB;
break;
default:
throw new Exception(String.format("传入的域ID[%s]不存在,请检查!", code));
}
return domain;
}
}

  以上两种都能实现,根据个人喜好选择,个人更倾向于实例1,代码结构更优美

最新文章

  1. PHP通过ini_set()来设置显示错误信息和执行时间
  2. 作业七:团队项目——Alpha版本冲刺阶段003
  3. python2.7.9基础学习
  4. 当EL遇到char
  5. 7.3---直线是否相交(CC150)
  6. 【Android开发经验】使用Ant批量打包Android应用全然指南
  7. 简单的前端js+ajax 购物车框架(入门篇)
  8. Atitit.dwr3 不能显示错误具体信息的解决方式,控件显示错误具体信息的解决方式 java .net php
  9. 大约sql声明优化
  10. linux_shell_类似sql的orderby 取最大值
  11. Selenium2Lib库之鼠标事件常用关键字实战
  12. BZOJ_2073_[POI2004]PRZ_状压DP
  13. python数据类型之字典类型
  14. HBase表数据的转移之使用自定义MapReduce
  15. usb设备驱动的分析
  16. swift 粒子效果
  17. css摘要
  18. VS中C#的快捷键
  19. 爬虫系列3:scrapy技术进阶(xpath、rules、shell等)
  20. python 字符串占位符的使用

热门文章

  1. linux设备树中如何删除某个节点?
  2. Introduction to statistical learning:with Applications in R (书,数据,R代码,链接)
  3. Oracle动态执行脚本创建序号
  4. 【Web网站服务器开发】Apache 和 Tomcat的区别及配置
  5. windows 3种方式运行exe文件
  6. python3正则表达式详细用法示例
  7. windows下安装Sonar
  8. AirFlow功能展示个人笔记
  9. [转帖]Java高级系列——注解(Annotations)
  10. 桥接模式下访问虚拟机中的Django项目