深入浅出讲解Java中的枚举类

时间:2022-09-04 21:45:57

背景:类的对象只有有限个,确定的。举例如下:
> 星期: Monday (星期一)、….、 Sunday (星期天)

> 性别: Man (男)、 Woman (女)
> 季节: Spring (春节).…….. Winter (冬天)
> 支付方式: Cash (现金)、 WeChatPay (微信)、 Alipay (支付宝) BankCard (银
行卡)、 CreditCard (信用卡)
> 就职状态: Busy 、 Free 、 Vocation 、 Dimission
> 订单状态: Nonpayment (未付款)、 Paid (已付款)、 Fulfilled (已配货)、
Delivered (已发货)、 Return 退货》 Checked 已确认)

> 线程状态:创建、就绪、运行、阻塞、死立

一、枚举类的使用

1.枚举类的理解:类的对象只有有限个,确定的。我们称此类为枚举类
2.当需要定义一组常量时,强烈建议使用枚举类
3.如果枚举类中只有一个对象,则可以作为单例模式的实现方式。

二、如何定义枚举类

方式一:JDK5.0之前,自定义枚举类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
public class SeasonTest {
    public static void main(String[] args) {
        Season spring = Season.SPRING;
        System.out.println(spring);
    }
}
    class Season {
        //1.声明Season对象的属性:private final修饰
        private final String seasonName;
        private final String seasonDesc;
        //2.私有化类的构造器,并给对象属性赋值
        private Season(String seasonName, String seasonDesc) {
            this.seasonName = seasonName;
            this.seasonDesc = seasonDesc;
        }
        //3.提供当前枚举类的对各对象:public static final的
        public static final Season SPRING = new Season("春天", "春暖花开");
        public static final Season SUMMER = new Season("夏天", "夏日炎炎");
        public static final Season AUTUMN = new Season("秋天", "秋高气爽");
        public static final Season WINTER = new Season("冬天", "冰天雪地");
        //4.其他诉求1:  获取枚举类对象的属性
        public String getSeasonName() {
            return seasonName;
        }
        public String getSeasonDesc() {
            return seasonDesc;
        }
        //4.其他诉求2:提供toString()
        @Override
        public String toString() {
            return "Season{" +
                    "seasonName='" + seasonName + '\'' +
                    ", seasonDesc='" + seasonDesc + '\'' +
                    '}';
        }
    }

方式二:JDK5.0之后,可以使用enum关键字定义枚举类

深入浅出讲解Java中的枚举类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public class SeasonTest1 {
    public static void main(String[] args) {
        Season1 summer = Season1.SUMMER;
        System.out.println(summer);
        System.out.println(Season1.class.getSuperclass());//java.lang.Enum
        //values():
        Season1[] values = Season1.values();
        for (int i = 0; i < values.length; i++) {
            System.out.println(values[i]);
        }
        //valueOf(String objName):返回枚举类中对象时objName的对象。
        Season1 winter = Season1.valueOf("WINTER");
        //如果没有objName的枚举类对象,则抛异常:IllegalArgumentException
//        Season1 winter = Season1.valueOf("WINTER1");
        System.out.println(winter);
//        winter.show();
    }
    //使用enum关键字定义枚举类
    enum Season1 {
        //1.提供当前枚举类的对象,多个对象之间用","隔开,末尾对象";"结束
        SPRING("春天", "春暖花开"),
        SUMMER("夏天", "夏日炎炎"),
        AUTUMN("秋天", "秋高气爽"),
        WINTER("冬天", "冰天雪地");
        //1.声明Season对象的属性:private final修饰
        private final String seasonName;
        private final String seasonDesc;
 
        //2.私有化类的构造器,并给对象属性赋值
        private Season1(String seasonName, String seasonDesc) {
            this.seasonName = seasonName;
            this.seasonDesc = seasonDesc;
        }
        //4.其他诉求1:  获取枚举类对象的属性
        public String getSeasonName() {
            return seasonName;
        }
        public String getSeasonDesc() {
            return seasonDesc;
        }

使用enum关键字定义的枚举类实现接口的情况
情况一:实现接口,在enum类中实现抽象方法

情况二:让枚举类的对象分别实现接口中的抽象方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public class SeasonTest1 {
    public static void main(String[] args) {
        //values():
        Season1[] values = Season1.values();
        for (int i = 0; i < values.length; i++) {
            System.out.println(values[i]);
            values[i].show();
        }
    }
    interface Info {
        void show();
    }
    enum Season1 implements Info {
        SPRING("春天", "春暖花开") {
            @Override
            public void show() {
                System.out.println("春天在哪里");
            }
        },
        SUMMER("夏天", "夏日炎炎") {
            @Override
            public void show() {
                System.out.println("宁夏");
            }
        },
        AUTUMN("秋天", "秋高气爽") {
            @Override
            public void show() {
                System.out.println("秋天不回来");
            }
        },
        WINTER("冬天", "冰天雪地") {
            @Override
            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;
        }
}

到此这篇关于深入浅出讲解Java中的枚举类的文章就介绍到这了,更多相关Java 枚举类内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_49329785/article/details/119656504