将代码中经常使用的常量,放在枚举中,是一个很好的编码习惯。便于统一修改,同时也增强代码的严谨和稳定性。当然,枚举的应用有很多,这里我只做一个简单的演示,以后看到有趣的使用,会慢慢丰富

package com.my.po;

/**
* description:{description}
* author:jyy
* date:2018-02-07 17:20
* modify:{modify}
*/
public enum Size { SMALL("S", "1"), MEDIUM("M", "2"), LARGE("L", "3"), EXTRA_LARGE("XL", "4"); private String abbreviation;
private String index; Size(String abbreviation, String index) {
this.abbreviation = abbreviation;
this.index = index;
} public String getAbbreviation() {
return abbreviation;
} public String getIndex() {
return index;
}
}

分析:

SMALL("S","1")执行构造函数Size(String abbreviation,String index)

getAbbreviation()方法获取SMALL("S","1")中的S值

getIndex()方法获取SMALL("S","1")中的1值

    @Test
public void test() { //查询SMALL的值
System.out.println(Size.SMALL.toString()); //toString()方法的逆方法valueOf(),s=Size.SMALL
Size s = Enum.valueOf(Size.class, "SMALL");
System.out.println(s.toString()); //获取SMALL中的abbreviation,index
System.out.println(Size.SMALL.getAbbreviation());
System.out.println(Size.SMALL.getIndex());
}

执行结果:

SMALL
SMALL
S
1

这两天无意之间看到一个枚举类TimeUnit,里面的部分代码如下:

public enum TimeUnit {
/**
* Time unit representing one thousandth of a microsecond
*/
NANOSECONDS {
public long toNanos(long d) { return d; }
public long toMicros(long d) { return d/(C1/C0); }
public long toMillis(long d) { return d/(C2/C0); }
public long toSeconds(long d) { return d/(C3/C0); }
public long toMinutes(long d) { return d/(C4/C0); }
public long toHours(long d) { return d/(C5/C0); }
public long toDays(long d) { return d/(C6/C0); }
public long convert(long d, TimeUnit u) { return u.toNanos(d); }
int excessNanos(long d, long m) { return (int)(d - (m*C2)); }
}, 。。。略。。。 public long toNanos(long duration) {
throw new AbstractMethodError();
} /**
* Equivalent to
* {@link #convert(long, TimeUnit) MICROSECONDS.convert(duration, this)}.
* @param duration the duration
* @return the converted duration,
* or {@code Long.MIN_VALUE} if conversion would negatively
* overflow, or {@code Long.MAX_VALUE} if it would positively overflow.
*/
public long toMicros(long duration) {
throw new AbstractMethodError();
} 。。。略。。。
}

最初不是很理解,后来在网上看到一个对枚举原理解释的帖子,顿时豁然开朗,下面我将简单介绍一下。

【举例】

public enum Size {

    SMALL("S","1"){
public String getSize(){
return "小号";
}
public String getRange(){
return "5-10";
}
},
MEDIUM("M","2"){
public String getSize(){
return "中号";
}
public String getRange(){
return "11-20";
}
},
LARGE("L","3"){
public String getSize(){
return "大号";
}
public String getRange(){
return "21-30";
}
}; public abstract String getSize();
public abstract String getRange(); private String abbreviation;
private String index; Size(String abbreviation, String index) {
this.abbreviation = abbreviation;
this.index = index;
} public String getAbbreviation() {
return this.abbreviation;
} public String getIndex() {
return this.index;
}
}
        System.out.println(Size.SMALL.getAbbreviation());
System.out.println(Size.SMALL.getIndex());
System.out.println(Size.SMALL.getSize());
System.out.println(Size.SMALL.getRange());

执行结果:

S
1
小号
5-10

当声明枚举类型Size的时候,其实是声明一个抽象类Size,同时也声明了抽象方法getSize()、getRange()。SMALL、MEDIUM、LARGE都是Size的匿名内部类(由satic final 关键字修饰),并重写了抽象方法。

可以将上面的枚举类Size,改写成以下方式:

public abstract class Size {

    public static final Size SMALL = new Size("S", "1") {
public String getSize() {
return "小号";
} public String getRange() {
return "5-10";
}
};
public static final Size MEDIUM = new Size("M", "2") {
public String getSize() {
return "中号";
} public String getRange() {
return "11-20";
}
};
public static final Size LARGE = new Size("L", "3") {
public String getSize() {
return "大号";
} public String getRange() {
return "21-30";
}
}; public abstract String getSize(); public abstract String getRange(); private String abbreviation;
private String index; Size(String abbreviation, String index) {
this.abbreviation = abbreviation;
this.index = index;
} public String getAbbreviation() {
return this.abbreviation;
} public String getIndex() {
return this.index;
}
}

最新文章

  1. iOS无限循环滚动scrollview
  2. linux下crontab命令的使用
  3. Yocto开发笔记之《快速入门,环境搭建 & 编译》(QQ交流群:519230208)
  4. sdutoj 2605 A^X mod P
  5. 洛谷P2722 总分 Score Inflation
  6. Stacked injection--堆叠注入--堆查询注入
  7. springMVC实现多文件上传
  8. SQL SERVER – Count Duplicate Records – Rows
  9. Android开源工具库
  10. 201521123035《Java程序设计》第四周学习总结
  11. Natas Wargame Level26 Writeup(PHP对象注入)
  12. C语言尝试在不同源文件中调用程序段
  13. Python3.6 连接MySQL(二)转载
  14. pdf.js使用总结#如何在网页读取并显示PDF格式文档
  15. python小白的自述
  16. 莫烦scikit-learn学习自修第二天【算法地图】
  17. vue里的样式添加之行间样式
  18. Code First 重复外键
  19. 网络之AFNetworking
  20. web本质

热门文章

  1. pmtk3
  2. [Vuex系列] - 细说state的几种用法
  3. Redis二进制安全概念
  4. WeixinJSBridge目前还能够直接使用的功能(2019)
  5. zookeeper:1
  6. 【问题】This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
  7. ubuntu---记录.opencv多版本管理与切换
  8. Window脚本学习笔记之BAT调用设置
  9. ndk学习之C语言基础复习----虚拟内存布局与malloc申请
  10. 从c到c++<二>