Java - 枚举(Enum)

  1. 云栖社区>
  2. 博客>
  3. 正文

Java - 枚举(Enum)

demon_coffee 2017-02-20 17:01:37 浏览637
Java - 枚举(Enum)

public class TestSeason1 {
 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();

 }
}
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("这是一个季节");
// }
}