什么是Java枚举?

时间:2013-10-05 20:45:29

标签: java enums

所以我不太了解枚举是什么,它是否像数组一样,可以拥有所有不同的数据类型,还可以在其中包含其他类似数组的数组?对不起,如果格式不是很好,但这是获得答案的唯一合适的方法。

3 个答案:

答案 0 :(得分:2)

在此引用官方Java文档:

枚举类型是一种特殊的数据类型,它使变量成为一组预定义的常量。变量必须等于为其预定义的值之一。常见示例包括罗盘方向(NORTH,SOUTH,EAST和WEST的值)和星期几。

请参阅http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

答案 1 :(得分:1)

创建文件名为EnumDay.java

的枚举
public enum EnumDay {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY,NAVEED
}

和文件名为EnumTest.java

的testclass
 public class EnumTest {
EnumDay day;

public EnumTest(EnumDay day) {
    this.day = day;
}

public void tellItLikeItIs() {
    switch (day) {
    case MONDAY:
        System.out.println("Mondays are bad.");
        break;

    case FRIDAY:
        System.out.println("Fridays are better.");
        break;

    case SATURDAY:
    case SUNDAY:
        System.out.println("Weekends are best.");
        break;

    case NAVEED:
        System.out.println(day + " is best.");
        break;

    default:
        System.out.println("Midweek days are so-so.");
        break;
    }
}

public static void main(String[] args) {
    EnumTest firstDay = new EnumTest(EnumDay.MONDAY);
    firstDay.tellItLikeItIs();
    EnumTest thirdDay = new EnumTest(EnumDay.WEDNESDAY);
    thirdDay.tellItLikeItIs();
    EnumTest fifthDay = new EnumTest(EnumDay.FRIDAY);
    fifthDay.tellItLikeItIs();
    EnumTest sixthDay = new EnumTest(EnumDay.SATURDAY);
    sixthDay.tellItLikeItIs();
    EnumTest seventhDay = new EnumTest(EnumDay.SUNDAY);
    seventhDay.tellItLikeItIs();
    EnumTest eigththDay = new EnumTest(EnumDay.NAVEED);
    eigththDay.tellItLikeItIs();
}
}

答案 2 :(得分:0)

java enum类似于类,但主要用于常量。它使用构造函数,就像一个类。我认为理解枚举如何工作的好方法是看一个例子。这是我在学习的过程中写的一个小例子。

    public class UseEnum {  
public enum Colors {
    RED("red", 1), BLUE("blue", 2), GREEN("green", 3), 
    YELLOW("yellow", 4), ORANGE("orange", 5), PURPLE("purple", 6), 
    PINK("pink", 7), WHITE("white", 8), BLACK("black", 9);
    private String color = "";
    private int id;
    private Colors(String color, int id) {
        this.color = color;
        this.id = id;
    }       
    private String getColor() {
        return color;
    }       
    private int getId() {
        return id;
    }
}
public static void main(String[] args) {
    while(true){    //infinite loop
        UseEnum ue = new UseEnum();
        ue.findColor(option);
    }
}
private static int option;  
private UseEnum() {
    try{
        Scanner s = new Scanner(System.in);
        System.out.print("Enter an ID: ");
        option = s.nextInt();
    }catch(Exception e){
        System.out.println("\terror: invalid number");
        System.exit(1);
    }
}   
private void findColor(int id) {
    boolean found = false;
    if(id == 10)
        System.exit(0);
    for(Colors c : Colors.values()) {
        if(c.getId() == id) {
            System.out.println("\t"+c.getColor());
            found = true;
            break;
        }
    }
    if(found != true)
        System.out.println("\tinvalid");
}

}

枚举可以在switch语句中使用,这使得它们非常有用。

我推荐这篇文章进一步阅读:javarevisited