JAVA基础知识|枚举
2024-09-02 07:35:57
将代码中经常使用的常量,放在枚举中,是一个很好的编码习惯。便于统一修改,同时也增强代码的严谨和稳定性。当然,枚举的应用有很多,这里我只做一个简单的演示,以后看到有趣的使用,会慢慢丰富
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;
}
}
最新文章
- iOS无限循环滚动scrollview
- linux下crontab命令的使用
- Yocto开发笔记之《快速入门,环境搭建 &; 编译》(QQ交流群:519230208)
- sdutoj 2605 A^X mod P
- 洛谷P2722 总分 Score Inflation
- Stacked injection--堆叠注入--堆查询注入
- springMVC实现多文件上传
- SQL SERVER – Count Duplicate Records – Rows
- Android开源工具库
- 201521123035《Java程序设计》第四周学习总结
- Natas Wargame Level26 Writeup(PHP对象注入)
- C语言尝试在不同源文件中调用程序段
- Python3.6 连接MySQL(二)转载
- pdf.js使用总结#如何在网页读取并显示PDF格式文档
- python小白的自述
- 莫烦scikit-learn学习自修第二天【算法地图】
- vue里的样式添加之行间样式
- Code First 重复外键
- 网络之AFNetworking
- web本质
热门文章
- pmtk3
- [Vuex系列] - 细说state的几种用法
- Redis二进制安全概念
- WeixinJSBridge目前还能够直接使用的功能(2019)
- zookeeper:1
- 【问题】This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
- ubuntu---记录.opencv多版本管理与切换
- Window脚本学习笔记之BAT调用设置
- ndk学习之C语言基础复习----虚拟内存布局与malloc申请
- 从c到c++<;二>;