企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# Java 枚举示例 > 原文: [https://javabeginnerstutorial.com/core-java-tutorial/java-enum-example/](https://javabeginnerstutorial.com/core-java-tutorial/java-enum-example/) ```java /** * @author GHajba * */ public class Main { public static void main(final String... args) { Schedule currentSchedule = new Schedule(); currentSchedule.setWorkday(Workday.FRIDAY); System.out.println("--------"); if (currentSchedule.getWorkday() == Workday.FRIDAY) { System.out.println("Hurray! Tomorrow is weekend!"); } System.out.println("--------"); switch (currentSchedule.getWorkday()) { case MONDAY: case TUESDAY: case WEDNESDAY: case THURSDAY: System.out.println("Working and working and..."); break; case FRIDAY: System.out.println("Hurray! Tomorrow is weekend!"); break; default: System.out.println("Unknown day -.-"); } System.out.println("--------"); System.out.println("Displaying all Workdays:"); for (Workday w : Workday.values()) { System.out.println(w); } System.out.println("--------"); System.out.println("Displaying all Workdays:"); for (Workday w : Workday.values()) { System.out.println(w.getRepresentation()); } } } ``` Schedule.java ```java /** * @author GHajba * */ public class Schedule { private Workday workday; public Workday getWorkday() { return this.workday; } public void setWorkday(final Workday workday) { this.workday = workday; } } ``` Workday.java ```java /** * @author GHajba * */ public enum Workday implements DatabaseEnum<Workday> { MONDAY("Monday"), TUESDAY("Tuesday"), WEDNESDAY("Wednesday"), THURSDAY("Thursday"), FRIDAY("Friday"); private final String representation; private Workday(final String representation) { this.representation = representation; } public String getRepresentation() { return this.representation; } @Override public String toString() { return getRepresentation(); } @Override public String toDatabaseString() { return this.representation; } @Override public Workday fromDatabase(final String representation) { for (Workday wd : Workday.values()) { if (wd.representation.equals(representation)) { return wd; } } return null; } } ```