Java之枚举类

一、 如何自定义枚举类?

二、 如何使用enum 创建枚举类以及枚举类的主要方法?

三、 如何实现接口的枚举类?

我通过下面的实例代码中解释这些问题吧,讲解请看注释。

先来看一下不使用enum 创建枚举类的方式吧,对比学习更快更轻松。

public class TestEnum {
    public static void main(String[] args) {
        Season spring  = Season.SPRING;
        System.out.println(spring);
    }
}
class Season{
    // 提供类的属性,声明为private final
    private String seasonName;
    private String seasonDesc;

     // 声明为final的属性,在构造器中初始化
    private Season(String seasonName,String seasonDesc){
        this.seasonName = seasonName;
        this.seasonDesc = seasonDesc;
    }
    public static  final Season SPRING = new Season("Spring","Spring");
    public static  final Season SUMMER = new Season("Summer","Summer");
    public static  final Season AUTUMN = new Season("Autumn","Autumn");
    public static  final Season WINTER = new Season("Winter","Winter");

    // 通过公共的方法来调用属性
    public String getSeasonName() {
        return seasonName;
    }

    public String getSeasonDesc() {
        return seasonDesc;
    }

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

下面是使用 Enum 创建的枚举类的方式:

/**
 * 常用的方法:values()、valueof()
 */
public class TestEnum2 {
    public static void main(String[] args) {
        Season2 spring  = Season2.SPRING;
        System.out.println(spring);
        System.out.println("-----------------");
        Season2 [] seasons = Season2.values();
        for (int i = 0; i < seasons.length; i++) {
            System.out.println(seasons[i]);
        }

        System.out.println("-----------------");
        String str = "AUTUMN";
        Season2 sea = Season2.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]);
        }

        System.out.println("-----------------");
        spring.show();
    }
}
// 枚举类
enum Season2{
    /**
     *  不需要接口的枚举实例
     * enum要求把枚举的对象放在最上面
     */
    //    SPRING("Spring","春"),
    //    SUMMER("Summer","夏"),
    //    AUTUMN("Autumn","秋"),
    //    WINTER("Winter","冬");

    // 下面是枚举类实现接口的实例
    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 String seasonName;
    private String seasonDesc;

    public void show(){
        System.out.println("这是一个季节");
    }
    private Season2(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 + '\'' +
                '}';
    }
}

 

你可能感兴趣的:(Java,Java基础系列)