枚举类

如果枚举类只有一个对象,则可以使用单例模式

  • 定义枚举类
    ① jdk5.0之前 自定义枚举类
    ② jdk5.0之后 使用Enum关键字
// 1.
public class EnumTest {
    public static void main(String[] args) {
        Season winter = Season.WINTER;
        System.out.println(winter.toString());  //Season{seasonName='冬天', seasonDec='冬天对象'}
    }
}
class Season{
    //1. 定义常量
    private final String seasonName;
    private final String seasonDec;

    //2. 私有化构造器
    private Season(String seasonName, String seasonDec){
        this.seasonName = seasonName;
        this.seasonDec = seasonDec;
    }

    // 3. 提供当前枚举类的多个对象
    public final static Season SPRING = new Season("春天","春天对象");
    public final static Season SUMMER = new Season("夏天","夏天对象");
    public final static Season AUTUMN = new Season("秋天","秋天对象");
    public final static Season WINTER = new Season("冬天","冬天对象");

    // 4. 其他需求 ,如:获取枚举类对象

    public String getSeasonName() {
        return seasonName;
    }

    public String getSeasonDec() {
        return seasonDec;
    }

    @Override
    public String toString() {
        return "Season{" +
                "seasonName='" + seasonName + '\'' +
                ", seasonDec='" + seasonDec + '\'' +
                '}';
    }
}

// 2.
public class EnumTest {
    public static void main(String[] args) {
        Season1 spring = Season1.SPRING;
        System.out.println(spring);  // SPRING
        System.out.println(Season1.class.getSuperclass()); //class java.lang.Enum ,其父类是Enum
    }
}

enum Season1{
    //  提供当前枚举类的多个 **对象** ,这一步 需要放前面
    SPRING("春天","春天对象"),  //这是一个对象
    SUMMER("夏天","夏天对象"),
    AUTUMN("秋天","秋天对象"),
    WINTER("冬天","冬天对象");

    //1. 定义常量
    private final String seasonName;
    private final String seasonDec;

    //2. 私有化构造器
    private Season1(String seasonName, String seasonDec){
        this.seasonName = seasonName;
        this.seasonDec = seasonDec;
    }

    // 4. 其他需求 ,如:获取枚举类对象

    public String getSeasonName() {
        return seasonName;
    }

    public String getSeasonDec() {
        return seasonDec;
    }

}
  • 枚举类可以实现接口
//编写一个接口
interface Info{
    void show();
}
//编写枚举类实现接口, 并重写方法
enum Season1 implements Info{
    // 3. 提供当前枚举类的多个对象
    SPRING("春天","春天对象"),
    SUMMER("夏天","夏天对象"),
    AUTUMN("秋天","秋天对象"),
    WINTER("冬天","冬天对象");

    //1. 定义常量
    private final String seasonName;
    private final String seasonDec;

    //2. 私有化构造器
    private Season1(String seasonName, String seasonDec){
        this.seasonName = seasonName;
        this.seasonDec = seasonDec;
    }

    // 4. 其他需求 ,如:获取枚举类对象

    public String getSeasonName() {
        return seasonName;
    }

    public String getSeasonDec() {
        return seasonDec;
    }

    //重写的方式,则每个对象都 做了一个事
    @Override
    public void show() {
        System.out.println("----------");
    }
}
//测试
      public static void main(String[] args) {
        Season1 spring = Season1.SPRING;
        System.out.println(spring);  //SPRING
        spring.show();  // ----------
    }


// 现在让枚举类中 每个对象都重写接口方法,则每个对象可以做自己的事
enum Season1 implements Info{
    // 3. 提供当前枚举类的多个对象
    SPRING("春天","春天对象"){
        @Override
        public void show() {
            System.out.println("spring对象做的事");
        }
    },
    SUMMER("夏天","夏天对象"){
        @Override
        public void show() {
            System.out.println("summer对象做的事");
        }
    },
    AUTUMN("秋天","秋天对象"){
        @Override
        public void show() {
            System.out.println("autumn对象做的事");
        }
    },
    WINTER("冬天","冬天对象"){
        @Override
        public void show() {
            System.out.println("winter对象做的事");
        }
    };

    //1. 定义常量
    private final String seasonName;
    private final String seasonDec;

    //2. 私有化构造器
    private Season1(String seasonName, String seasonDec){
        this.seasonName = seasonName;
        this.seasonDec = seasonDec;
    }

    // 4. 其他需求 ,如:获取枚举类对象

    public String getSeasonName() {
        return seasonName;
    }

    public String getSeasonDec() {
        return seasonDec;
    }

}
//测试
        Season1 spring = Season1.SPRING;
        System.out.println(spring);  //SPRING
        spring.show();  //spring对象做的事
posted @ 2021-01-31 17:18  先生胡  阅读(53)  评论(0编辑  收藏  举报