package enums;
public class Enum
{
//enum 枚举
public enum Color
{
RED, GREEN, BLANK, YELLOW
}
public enum Colors
{
REDs("红色", 1), GREENs("绿色", 2), BLANKs("白色", 3), YELLOs("黄色", 4);
private String name;
private int index;
private Colors(String name, int index)
{
this.name = name;
this.index = index;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getIndex()
{
return index;
}
public void setIndex(int index)
{
this.index = index;
}
}
public static void main(String[] args)
{
// -------------------1
System.out.println(Color.BLANK);
System.out.println(Color.RED.ordinal());//返回枚举值在enum类中的顺序
System.out.println(Color.RED.compareTo(Color.BLANK));//比较
System.out.println(Color.RED.toString());
Color[] colors = Color.values();//values():返回一个包含全部枚举值的数组
System.out.println(colors[1]);
// ------------------------2
System.out.println("\r\n");
System.out.println("--------2----------------");
// 输出某一枚举的值
System.out.println(Colors.REDs.getName());
System.out.println(Colors.REDs.getIndex());
// 遍历所有的枚举
for (Colors color : Colors.values())
{
System.out.println(color + " name: " + color.getName() + " index: " + color.getIndex());
}
}
}