一文带你入门Java的【枚举类】

一、什么是枚举类

枚举类型用于声明一组命名的常数,当一个变量只有几种已知的取值时,可以将它定义为枚举类型。

例如:

  • 每星期的星期一到星期日
  • 每月的一月到十二月
  • 四季的春夏秋冬

二、自定义枚举类

创建步骤

  1. 声明对象的属性
  2. 提供私有构造函数
  3. 提供当前枚举类中的对象
  4. 提供Get方法/重写toString()
/**
 * 自定义枚举类
 */
class Week {
    // 1. 声明Week对象属性
    private final String weekName;
    private final String color;

    //2. 提供私有构造函数
    private Week(String weekName, String color) {
        this.weekName = weekName;
        this.color = color;
    }

    //3. 提供当前枚举类中的对象
    public static final Week Monday = new Week("星期一", "黑色");
    public static final Week Tuesday = new Week("星期二", "绿色");
    public static final Week Wednesday = new Week("星期三", "黄色");
    public static final Week Thursday = new Week("星期四", "蓝色");
    public static final Week Friday = new Week("星期五", "红色");
    public static final Week Saturday = new Week("星期六", "青色");
    public static final Week Sunday = new Week("星期日", "白色");

    //4.提供get方法获取枚举类的属性
    public String getWeekName() {
        return weekName;
    }
    public String getColor() {
        return color;
    }
    //5. 重写toString()
    @Override public String toString() {
        return "Week{weekName='" + weekName + ", color='" + color + "'}";
    }
}

三、使用enum关键字枚举类

创建步骤

  1. 提供当前枚举类对象,多个对象之间用 ‘,’ 隔开,末尾用 ‘;’ 结束
  2. 声明对象的属性
  3. 提供构造函数
  4. 提供当前枚举类中的对象
  5. 提供Get方法/重写toString()
/**
 * 使用enum关键字枚举类
 */
enum EWeek {
    // 1. 提供当前枚举类对象,多个对象之间用 ',' 隔开,末尾用 ';' 结束
    Monday("星期一", "黑色"),
    Tuesday("星期二", "绿色"),
    Wednesday("星期三", "黄色"),
    Thursday("星期四", "蓝色"),
    Friday("星期五", "红色"),
    Saturday("星期六", "青色"),
    Sunday("星期日", "白色");

    // 2. 声明Week对象属性
    private final String weekName;
    private final String color;

    // 3. 构造函数,给对象赋值
    EWeek(String weekName, String color) {
        this.weekName = weekName;
        this.color = color;
    }

    //4.获取枚举类的属性
    public String getWeekName() {
        return weekName;
    }
    public String getColor() {
        return color;
    }
    //5. 重写toString()
    @Override public String toString() {
        return "Week{weekName='" + weekName + ", color='" + color + "'}";
    }
}

四、枚举类常用方法

  1. toString():输出对象
  2. values():返回所的枚举类对象构成的数组
  3. valueOf(String objName):返回枚举类中对象名是objName的对象。
public class EnumDemo {
    public static void main(String[] args) {
        EWeek week = EWeek.Monday;
        //1. 调用toString
        System.out.println("--------------toString()-------------");
        System.out.println(week);
        //2. 返回所的枚举类对象构成的数组
        System.out.println("------------values()---------------");
        EWeek[] weeks = EWeek.values();
        System.out.println(Arrays.toString(weeks));
        System.out.println("-------------valueOf(String objName)--------------");
        //3. valueOf(String objName):返回枚举类中对象名是objName的对象。
        EWeek week1 = EWeek.valueOf("Monday");
        System.out.println(week1);
        //如果没objName的枚举类对象,则抛异常:IllegalArgumentException
        System.out.println("--------------测试无objName对象-------------");
        EWeek week2 = EWeek.valueOf("aaaa");
    }
}

输出结果

在这里插入图片描述

五、让enum枚举类对象分别实现接口

public class EnumInterfaceDemo {
    public static void main(String[] args) {
        EWeeks eWeeks = EWeeks.Monday;
        eWeeks.show();
    }
}

interface Info{
    void show();
}

/**
 * 使用enum关键字枚举类
 */
enum EWeeks implements Info{
    // 1. 提供当前枚举类对象,多个对象之间用 ',' 隔开,末尾用 ';' 结束
    Monday("星期一", "黑色"){
        @Override public void show() {
            System.out.println(this.getWeekName() + " 是 " + this.getColor());
        }
    },
    Tuesday("星期二", "绿色"){
        @Override public void show() {
            System.out.println(this.getWeekName() + " 是 " + this.getColor());
        }
    },
    Wednesday("星期三", "黄色"){
        @Override public void show() {
            System.out.println(this.getWeekName() + " 是 " + this.getColor());
        }
    },
    Thursday("星期四", "蓝色"){
        @Override public void show() {
            System.out.println(this.getWeekName() + " 是 " + this.getColor());
        }
    },
    Friday("星期五", "红色"){
        @Override public void show() {
            System.out.println(this.getWeekName() + " 是 " + this.getColor());
        }
    },
    Saturday("星期六", "青色"){
        @Override public void show() {
            System.out.println(this.getWeekName() + " 是 " + this.getColor());
        }
    },
    Sunday("星期日", "白色"){
        @Override public void show() {
            System.out.println(this.getWeekName() + " 是 " + this.getColor());
        }
    };

    // 2. 声明Week对象属性
    private final String weekName;
    private final String color;

    // 3. 构造函数,给对象赋值
    EWeeks(String weekName, String color) {
        this.weekName = weekName;
        this.color = color;
    }

    //4.获取枚举类的属性
    public String getWeekName() {
        return weekName;
    }
    public String getColor() {
        return color;
    }
    //5. 重写toString()
    @Override public String toString() {
        return "Week{weekName='" + weekName + ", color='" + color + "'}";
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章