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("藍(lán)色");
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("藍(lán)色");
break;
}