一尘不染

在Java中开启Enum

java

为什么不能打开Java枚举?看起来很简单,可以做一些方便的代码。这个问题也适用于String。你可以打开char,但不能打开String...


阅读 398

收藏
2020-03-24

共1个答案

一尘不染

你绝对可以打开枚举。Java教程中发布的示例。

public enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, 
    THURSDAY, FRIDAY, SATURDAY 
}

public class EnumTest {
    Day day;

    public EnumTest(Day 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;

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