枚举
一、基础入门
1.常用范围
一般用于定义一组相类似的常量
2.古老方式定义枚举类
public class TestOne {
public static void main(String[] args) {
System.out.println(People.LIU);
System.out.println(People.LI);
}
}
/**
* 自定义枚举类
*/
class People {
private final String name;
private final Integer age;
People(String name, Integer age) {
this.name = name;
this.age = age;
}
public static final People LIU = new People("liu", 12);
public static final People LI = new People("li", 23);
@Override
public String toString() {
return "People{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
3.Enum关键字定义
3.1 默认覆盖
/**
* @author hollow
* 枚举类写法
*/
public class TestOne {
public static void main(String[] args) {
System.out.println(People.LIU);
System.out.println(People.LI);
//补充getClass和getDeclaringClass的区别 看getDeclaringClass源码
System.out.println(People.LI.getDeclaringClass());
System.out.println(People.LI.getClass());
}
}
/**
* 自定义枚举类
*/
enum People {
/**
* 人物信息
*/
LIU("liu",12),
LI("li",24);
private final String name;
private final Integer age;
People(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
}
结果图
差异主要看源码,源码如下
public final Class<E> getDeclaringClass() {
Class<?> clazz = getClass();
Class<?> zuper = clazz.getSuperclass();
return (zuper == Enum.class) ? (Class<E>)clazz : (Class<E>)zuper;
}
多判定更加稳定
3.2 手动覆盖
@Override
public String toString() {
return "People{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
结果图
二、常用方法
1.values()
/**
* @author hollow
* 枚举类写法
*/
public class TestOne {
public static void main(String[] args) {
System.out.println(People.LIU);
System.out.println(People.LI);
//补充getClass和getDeclaringClass的区别 看getDeclaringClass源码
System.out.println(People.LI.getDeclaringClass());
System.out.println(People.LI.getClass());
System.out.println("----------------------------------------");
for (People value : People.values()) {
System.out.println(value);
}
}
}
/**
* 自定义枚举类
*/
enum People {
/**
* 人物信息
*/
LIU("liu",12),
LI("li",24);
private final String name;
private final Integer age;
People(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
}
结果图
PS:个人理解,就是通过values()将自定义枚举类变成一个数组
2.valueOf()
很简单…就是在括号内输入想要的对象名,得到该对象
System.out.println("---------------------------------------");
People liu = People.valueOf("LIU");
System.out.println(liu);
结果图
3.toString()
System.out.println("---------------------------------------");System.out.println(People.LI.toString());
结果图
三、接口实现
1.接口实现
/**
* @author hollow
* 枚举类接口实现
*/
public class TestOne {
public static void main(String[] args) {
People.valueOf("LIU").show();
People.valueOf("LI").show();
People.LIU.show();
People.LI.show();
System.out.println(People.LI.getDeclaringClass());
System.out.println(People.LI.getClass());
}
}
/**
* 详细展示
*/
interface DetailInfo {
/**
* 详细显示
*/
void show();
}
/**
* 自定义枚举类
*/
enum People implements DetailInfo{
/**
* 人物信息
*/
LIU("liu",12){
@Override
public void show() {
System.out.println("溜溜溜");
}
},
LI("li",24) {
@Override
public void show() {
System.out.println("莉莉力");
}
};
private final String name;
private final Integer age;
People(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
}
2.结果图
这里我们就可以发现,上面我提到的getDeclaringClass
和getClass
的区别了。
参考:https://www.bilibili.com/video/BV1Kb411W75N?p=502