/** * @Author Tiam * @Date 2021/12/27 18:00 * @Description: 枚举类的使用 */ public enum Enum_ { //注意分割符,最后一个以分号结尾. MONDAY("星期一"), TUESDAY("星期二"), WEDNESDAY("星期三"), THURSDAY("星期四"), FRIDAY("星期五"), SATURDAY("星期六"), SUNDAY("星期日"); /** * 星期 */ private String week; /** * 构造器 * @param week */ Enum_(String week) { this.week = week; } /** * get,set方法 * @return */ public String getWeek() { return week; } public void setWeek(String week) { this.week = week; } }
/** * @Author Tiam * @Date 2021/12/27 20:17 * @Description: 枚举与switch语句的配合使用 */ public class Burrito { Enum_ em; Burrito() { } public Burrito(Enum_ em) { this.em = em; } /** * 枚举与switch语句的配合使用 * @return */ String describe() { switch (em) { case MONDAY: return Enum_.MONDAY.getWeek(); case TUESDAY: return Enum_.TUESDAY.getWeek(); case WEDNESDAY: return Enum_.WEDNESDAY.getWeek(); case THURSDAY: return Enum_.THURSDAY.getWeek(); case FRIDAY: return Enum_.FRIDAY.getWeek(); case SATURDAY: return Enum_.SATURDAY.getWeek(); case SUNDAY: return Enum_.SUNDAY.getWeek(); default: throw new IllegalArgumentException("非法参数"); } } public static void main(String[] args) { System.out.println(new Burrito().em); System.out.println(Enum_.FRIDAY); System.out.println(Enum_.FRIDAY.getWeek()); Burrito b1 = new Burrito(Enum_.MONDAY), b2 = new Burrito(Enum_.SUNDAY), b3 = new Burrito(Enum_.THURSDAY); String d1 = b1.describe(); String d2 = b2.describe(); String d3 = b3.describe(); System.out.println(d1); System.out.println(d2); System.out.println(d3); /* Output: null FRIDAY 星期五 星期一 星期日 星期四 */ } }