以下是一个简单的 Java 枚举类的示例代码:
enum Day { MONDAY("Monday"), TUESDAY("Tuesday"), WEDNESDAY("Wednesday"), THURSDAY("Thursday"), FRIDAY("Friday"), SATURDAY("Saturday"), SUNDAY("Sunday"); private final String name; private Day(String name) { this.name = name; } public String getName() { return name; } public boolean isWeekend() { return this == SATURDAY || this == SUNDAY; } } public class Test{ public static void main(String[] args) { for (Day day : Day.values()) { System.out.println(day); // System.out.println(day.isWeekend()); } } }
输出的结果为:
MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY SUNDAY