Java枚举的用法有以下几种:
enum Color {
RED, GREEN, BLUE;
}
Color color = Color.RED;
for(Color color : Color.values()) {
System.out.println(color);
}
int ordinal = Color.RED.ordinal();
Color color1 = Color.RED;
Color color2 = Color.RED;
boolean isEqual = color1.equals(color2);
enum Color {
RED("红色"), GREEN("绿色"), BLUE("蓝色");
private String name;
private Color(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
String name = Color.RED.getName();
Color color = Color.RED;
switch(color) {
case RED:
System.out.println("红色");
break;
case GREEN:
System.out.println("绿色");
break;
case BLUE:
System.out.println("蓝色");
break;
}