enum Colors {
Red(1), Yellow(2), Blue(3);
private int _value;
private Colors(int value)
{
_value = value;
}
public int value()
{
return _value;
}
}
public class TestEnum {
public static void main(String[] args) {
int result = Colors.Blue.value();//获取枚举元素对应的值,即()里面的值
System.out.println(result);
Colors colors = Colors.Red;
Colors color = Enum.valueOf(Colors.class, "Red");//字符串转换为枚举
switch (color) {
case Red:
System.out.println("红色");
break;
case Yellow:
System.out.println("黄色");
break;
case Blue:
System.out.println("蓝色");
break;
default:
break;
}
System.out.println(Colors.Red.ordinal());//输出枚举元素对应的序数,第一个对应0以此类推
//遍历枚举
for (Colors c : Colors.values()) {
System.out.println(c);
}
}
}