* 一、枚举类
* 1.如何自定义枚举类
* 2.如何使用enum关键字定义枚举类
*
>常用的方法:values() valueOf(String name)
*
>如何让枚举类实现接口:可以让不同的枚举类的对象调用被重写的抽象方法,执行的效果不同。(相当于让每个对象重写抽象方法)
1 //普通枚举类 2 class Season{ 3 //1.提供类的属性,声明为private final 4 private final String seasonName; 5 private final String seasonDesc; 6 //2.声明为final的属性,在构造器中初始化。 7 private Season(String seasonName,String seasonDesc){ 8 this.seasonName = seasonName; 9 this.seasonDesc = seasonDesc; 10 } 11 //3.通过公共的方法来调用属性 12 public String getSeasonName() { 13 return seasonName; 14 } 15 public String getSeasonDesc() { 16 return seasonDesc; 17 } 18 //4.创建枚举类的对象:将类的对象声明public static final 19 public static final Season SPRING = new Season("spring", "春暖花开"); 20 public static final Season SUMMER = new Season("summer", "夏日炎炎"); 21 public static final Season AUTUMN = new Season("autumn", "秋高气爽"); 22 public static final Season WINTER = new Season("winter", "白雪皑皑"); 23 @Override 24 public String toString() { 25 return "Season [seasonName=" + seasonName + ", seasonDesc=" 26 + seasonDesc + "]"; 27 } 28 public void show(){ 29 System.out.println("这是一个季节"); 30 } 31 }
//自定义枚举类 interface Info{ void show(); } //枚举类 enum Season1 implements Info{ SPRING("spring", "春暖花开"){ public void show(){ System.out.println("春天在哪里?"); } }, SUMMER("summer", "夏日炎炎"){ public void show(){ System.out.println("生如夏花"); } }, AUTUMN("autumn", "秋高气爽"){ public void show(){ System.out.println("秋天是用来分手的季节"); } }, WINTER("winter", "白雪皑皑"){ public void show(){ System.out.println("冬天里的一把火"); } }; private final String seasonName; private final String seasonDesc; private Season1(String seasonName,String seasonDesc){ this.seasonName = seasonName; this.seasonDesc = seasonDesc; } public String getSeasonName() { return seasonName; } public String getSeasonDesc() { return seasonDesc; } @Override public String toString() { return "Season [seasonName=" + seasonName + ", seasonDesc=" + seasonDesc + "]"; } // public void show(){ // System.out.println("这是一个季节"); // } } public static void main(String[] args) { Season1 spring = Season1.SPRING; System.out.println(spring); spring.show(); System.out.println(spring.getSeasonName()); System.out.println(); //1.values() Season1[] seasons = Season1.values(); for(int i = 0;i < seasons.length;i++){ System.out.println(seasons[i]); } //2.valueOf(String name):要求传入的形参name是枚举类对象的名字。 //否则,报java.lang.IllegalArgumentException异常 String str = "WINTER"; Season1 sea = Season1.valueOf(str); System.out.println(sea); System.out.println(); Thread.State[] states = Thread.State.values(); for(int i = 0;i < states.length;i++){ System.out.println(states[i]); } sea.show(); }