1.枚举类的理解:类的对象只有有限个,确定的
2.当需要定义一组常量时,强烈建议使用枚举类
3.如果枚举类中只有一个对象,则可以作为单例模式的实现方式
package com.klvchen.java; public class SeasonTest1 { public static void main(String[] args){ Season1 summer = Season1.SUMMER; System.out.println(summer); System.out.println(summer.toString()); System.out.println(Season1.class.getSuperclass()); System.out.println("********************"); Season1[] values = Season1.values(); for (int i = 0; i < values.length; i++){ System.out.println(values[i]); values[i].show(); } System.out.println("********************"); Thread.State[] values1 = Thread.State.values(); for (int i = 0; i < values1.length; i++){ System.out.println(values1[i]); } System.out.println("********************"); Season1 winter = Season1.valueOf("WINTER"); System.out.println(winter); winter.show(); } } interface Info{ void show(); } //使用 enum 关键字枚举类 enum Season1 implements Info{ //1.提供当前枚举类的对象,多个对象之间用","隔开,末尾对象";"结束 SPRING("春天","春暖花开"){ @Override public void show(){ System.out.println("春天在哪里?"); } }, SUMMER("夏天","夏日炎炎"){ @Override public void show(){ System.out.println("宁夏"); } }, AUTUMN("秋天","秋高气爽"){ @Override public void show(){ System.out.println("秋天不回来"); } }, WINTER("冬天","冰天雪地"){ @Override public void show(){ System.out.println("大约在冬季"); } }; //2.声明 Season 对象的属性: private final 修饰 private final String seasonName; private final String seasonDesc; //3.私有化类的构造器,并给对象属性赋值 private Season1(String seasonName,String seasonDesc){ this.seasonName = seasonName; this.seasonDesc = seasonDesc; } public String getSeasonName(){ return seasonName; } public String getSeasonDesc(){ return seasonDesc; } @Override public void show(){ System.out.println("这是一个季节"); } }