枚举的英文(enumeration,简称 enum),是一组常量的集合,属于一种特殊的类,里面只包含一组有限的特定的对象。
自定义类实现枚举
class Season { public final static Season SUMMER = new Season("夏天"); public final static Season WINTER = new Season("冬天"); private String name; private Season(String name) { this.name = name; } public String getName() { return name; } }
注意事项:
final
+static
共同修饰,实现底层优化(我们直接使用final 修饰的静态属性的时候,不会导致类被加载。enum
关键字实现枚举
package com.smile.cms.enums; public enum Season { SUMMER("夏天"), WINTER("冬天"); private String name; private Season(String name) { this.name = name; } public String getName() { return name; } }
java.lang.Enum
类声明
public abstract class Enum<E extends Enum<E>> implements Constable, Comparable<E>, Serializable
编译Season
类,生成Season.class
javac Season.java
反编译Season.class
类
javap Season.class
Compiled from "Season.java" public final class com.smile.cms.enums.Season extends java.lang.Enum<com.smile.cms.enums.Season> { public static final com.smile.cms.enums.Season SUMMER; public static final com.smile.cms.enums.Season WINTER; public static com.smile.cms.enums.Season[] values(); public static com.smile.cms.enums.Season valueOf(java.lang.String); public java.lang.String getName(); static {}; }
从上面可以看出,枚举的本质是java.lang.Enum
的子类。尽管enum
是一种受限制的类,并且具有自己的方法。枚举这种特殊的类,因为被修饰为final
,所以不能继承其他的类。
创建一个枚举
package enums; public enum Season { WINTER("冬天"), SUMMER("夏天"); private String name; Season(String name){ this.name = name; } public String getName() { return this.name; } }
values
返回enum实例的数组
public class Application { public static void main(String[] args) { Season[] seasons = Season.values(); for (Season season : seasons) { System.out.println(season.getName()); } } }
输出结果:冬天 夏天
name
返回实例名
public class Application { public static void main(String[] args) { System.out.println(Season.SUMMER); } }
输出结果:SUMMER
ordinal
返回实例声明时的次序,从0
开始
public class Application { public static void main(String[] args) { System.out.println(Season.WINTER.ordinal()); System.out.println(Season.SUMMER.ordinal()); } }
输出结果:0 1
getDeclaringClass
返回实例所属的enum
类型
public class Application { public static void main(String[] args) { System.out.println(Season.WINTER.getDeclaringClass()); } }
输出结果:class enums.Season
equals
判断是否为同一个对象
public class Application { public static void main(String[] args) { System.out.println(Season.WINTER.equals(Season.WINTER)); } }
输出的结果:true
valueOf
根据字符串获取到实例
public class Application { public static void main(String[] args) { System.out.println(Season.valueOf("SUMMER").getName()); } }
除了不能继承,基本上可以将
enum
看做一个常规的类
枚举对象放置在代码的最前面
多个枚举对象通过,
进行展示,最后一个枚举对象以;
结束
枚举值默认从0
开始的有序数值
枚举可以添加普通方法
,静态方法
,抽象方法
,构造方法
package enums; public enum ResponseCode { SUCCESS(200){ @Override public String getDescription() { return "成功"; } }; private int code; //构造方法 private ResponseCode(int code){ this.code = code; } //普通方法 public int getCode() { return code; } //抽象方法 public abstract String getDescription(); }
枚举可以实现接口
package enums; public interface ICode { public int getCode(); }
package enums; public enum ResponseCode implements ICode{ SUCCESS(200){ @Override public String getDescription() { return "成功"; } }; private int code; private ResponseCode(int code){ this.code = code; } public int getCode() { return code; } public abstract String getDescription(); }
枚举不可以继承
enum不能被继承
enum 类底层被修饰成final
类
enum不能继承
enum 类底层,已经继承了java.lang.Enum
public final class com.smile.cms.enums.Season extends java.lang.Enum<com.smile.cms.enums.Season>
组织常
switch
状态判断
public static String getTrafficInstruct(Signal signal) { String instruct = "信号灯故障"; switch (signal) { case RED: instruct = "红灯停"; break; case YELLOW: instruct = "黄灯请注意"; break; case GREEN: instruct = "绿灯行"; break; default: break; } return instruct; }
状态码
组织枚举
可以将类型相近的枚举通过接口或类组织起来,但是一般用接口进行组织。
package enums; public class ResponseCode { public interface ICode{ int getCode(); String getMessage(); } public interface Code{ enum HttpCode implements ICode{ SUCCESS(200,"接口请求成功"), ERROR(400,"接口请求失败"); HttpCode(int code,String message){ this.code = code; this.message = message; } private int code; private String message; @Override public int getCode() { return code; } @Override public String getMessage() { return message; } } enum CustomerCode implements ICode{ ERROR_LOGIN(50001,"登陆异常"); private int code; private String message; CustomerCode(int code,String message){ this.code = code; this.message = message; } @Override public int getCode() { return code; } @Override public String getMessage() { return message; } } } }