Java 枚举
枚举的定义、带字段与方法的枚举、遍历与 switch 配合。
Key Points枚举是固定常量集合,本质是继承 Enum 的 final 类;可以带字段、构造方法和方法,比 int 常量更安全。
枚举的定义
An enum lists constants separated by commas. Each constant is a public static final instance; values() returns them all and valueOf() looks one up by name.
Java
enum Color {
RED, GREEN, BLUE
}
public class EnumBasic {
public static void main(String[] args) {
Color c = Color.GREEN;
System.out.println(c);
System.out.println(c.ordinal());
System.out.println(Color.valueOf("BLUE"));
for (Color x : Color.values()) {
System.out.print(x + " ");
}
System.out.println();
}
}Output
GREEN
1
BLUE
RED GREEN BLUE带字段和方法的枚举
Enums may declare a constructor (implicitly private), fields and methods so each constant carries data — very handy for code-plus-label pairs.
Java
enum Status {
OK(200, "success"),
NOT_FOUND(404, "not found"),
ERROR(500, "server error");
private final int code;
private final String label;
Status(int code, String label) {
this.code = code;
this.label = label;
}
public int getCode() {
return code;
}
public String getLabel() {
return label;
}
public boolean isError() {
return code >= 400;
}
}
public class EnumFields {
public static void main(String[] args) {
System.out.println(Status.OK.getCode());
System.out.println(Status.NOT_FOUND.getLabel());
System.out.println(Status.ERROR.isError());
}
}Output
200
not found
true枚举与 switch
switch accepts enums; write constant names in case without the class prefix. The Java 14 arrow form is terser and the compiler can flag missing branches.
Java
enum Level { LOW, MEDIUM, HIGH }
public class EnumSwitch {
public static void main(String[] args) {
Level lv = Level.MEDIUM;
switch (lv) {
case LOW -> System.out.println("low");
case MEDIUM -> System.out.println("medium");
case HIGH -> System.out.println("high");
}
int score = switch (lv) {
case LOW -> 1;
case MEDIUM -> 2;
case HIGH -> 3;
};
System.out.println(score);
}
}Output
medium
2枚举的定义
enum 用逗号分隔常量,最后一个后面可选分号。常量本身是 public static final 的实例,values() 返回全部,valueOf() 按名字取。
Java
enum Color {
RED, GREEN, BLUE
}
public class EnumBasic {
public static void main(String[] args) {
Color c = Color.GREEN;
System.out.println(c);
System.out.println(c.ordinal());
System.out.println(Color.valueOf("BLUE"));
for (Color x : Color.values()) {
System.out.print(x + " ");
}
System.out.println();
}
}Output
GREEN
1
BLUE
RED GREEN BLUE带字段和方法的枚举
枚举可以有构造方法(隐式 private)、字段和普通方法,用来给常量附带数据。这在需要「码值 + 描述」的场景非常实用。
Java
enum Status {
OK(200, "success"),
NOT_FOUND(404, "not found"),
ERROR(500, "server error");
private final int code;
private final String label;
Status(int code, String label) {
this.code = code;
this.label = label;
}
public int getCode() {
return code;
}
public String getLabel() {
return label;
}
public boolean isError() {
return code >= 400;
}
}
public class EnumFields {
public static void main(String[] args) {
System.out.println(Status.OK.getCode());
System.out.println(Status.NOT_FOUND.getLabel());
System.out.println(Status.ERROR.isError());
}
}Output
200
not found
true枚举与 switch
switch 支持枚举,case 里直接写常量名、不加枚举类前缀。Java 14 的箭头写法更简洁,且编译器会提示你漏掉的分支。
Java
enum Level { LOW, MEDIUM, HIGH }
public class EnumSwitch {
public static void main(String[] args) {
Level lv = Level.MEDIUM;
switch (lv) {
case LOW -> System.out.println("low");
case MEDIUM -> System.out.println("medium");
case HIGH -> System.out.println("high");
}
int score = switch (lv) {
case LOW -> 1;
case MEDIUM -> 2;
case HIGH -> 3;
};
System.out.println(score);
}
}Output
medium
2