JavaSE---枚举类

1、Why?为什么要有枚举类?

    a, 手动实现枚举类,代码量较大,实现比较麻烦;

    b, 使用常量表示 枚举,存在的问题:

      类型不安全;对象的意义不明确;

1
2
private static final int SPRING = 1;// 春天
    private static final int SUMMER = 2;// 夏天

2、What?

    2.1、JDK5后 ,Java提供了enum关键字;

    2.2、枚举类 与 普通类的区别:

        a, 使用enum定义的类 默认继承java.lang.Enum类,不是Object;

        b, 枚举类的构造器 只能使用private修饰,如果省略了修饰符,默认使用private;

        c, 枚举类的所有实例必须显式指出,实例默认会添加 public static fianl,无需显式指定;

        d, 所有枚举类都提供一个values(),很方便的遍历所有的枚举值;

3、How?

    3.1、所有 枚举值之间使用 英文逗号 分隔,枚举值结束 用 英文分号 结束

    3.2、switch中使用枚举,只需要 使用枚举值的名字,无需添加枚举类作为限定;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public enum EnumTest {
 
    SPRING,SUMMER;
 
}
 
 
 
import static com.an.EnumTest.SPRING;
 
public class Test {
 
   public static void main(String[] args) {
      EnumTest o= null ;
 
        switch (o){
            case SPRING :
                break;
        }
   }
 
}   

4、Enum类

    4.1、Java中所有的枚举类 默认都继承 Enum类;

    4.2、Eunm实现了Comparable、Serializable 2个接口;

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
public abstract class Enum<E extends Enum<E>>
        implements Comparable<E>, Serializable {
 
    // 枚举值常量的名称,程序员应该使用toString(),避免直接访问name字段
    private final String name;
 
    // 枚举值常量的序号
    private final int ordinal;
 
    protected Enum(String name, int ordinal) {
        this.name = name;
        this.ordinal = ordinal;
    }
 
    public final String name() {
        return name;
    }
 
    public final int ordinal() {
        return ordinal;
    }
 
    public String toString() {
        return name;
    }
 
    public final boolean equals(Object other) {
        return this==other;
    }
 
    public final int hashCode() {
        return super.hashCode();
    }
 
    public final int compareTo(E o) {
        Enum<?> other = (Enum<?>)o;
        Enum<E> self = this;
        if (self.getClass() != other.getClass() && // optimization
            self.getDeclaringClass() != other.getDeclaringClass())
            throw new ClassCastException();
        return self.ordinal - other.ordinal;
    }
 
    public static <T extends Enum<T>> T valueOf(Class<T> enumType,
                                                String name) {
        T result = enumType.enumConstantDirectory().get(name);
        if (result != null)
            return result;
        if (name == null)
            throw new NullPointerException("Name is null");
        throw new IllegalArgumentException(
            "No enum constant " + enumType.getCanonicalName() + "." + name);
    }
 
    private void readObject(ObjectInputStream in) throws IOException,
        ClassNotFoundException {
        throw new InvalidObjectException("can't deserialize enum");
    }
 
    private void readObjectNoData() throws ObjectStreamException {
        throw new InvalidObjectException("can't deserialize enum");
    }
 
}

5、实现接口的枚举类

6、包含抽象方法的枚举类

7、拓展

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
package com.an.enumtest;
 
public enum UserEnum {
 
    MAN,WOMAN;
 
    /**
     * public final class com.an.enumtest.UserEnum extends java.lang.Enum<com.an.enumtest.UserEnum>
     *
     *     public static final com.an.enumtest.UserEnum MAN;
     *
     *     public static final com.an.enumtest.UserEnum WOMAN;
     *
     *     public static com.an.enumtest.UserEnum[] values();
     *
     *     public static com.an.enumtest.UserEnum valueOf(java.lang.String);
     */
 
    /**
     * 1、枚举类 默认继承 java.lang.Enum,编译后 为 public final class *** extends java.lang.Enum
     * 2、枚举类的 元素 作为 内部类存在 public static final com.an.enumtest.UserEnum MAN;
     * 3、枚举类的 元素 在JVM中 唯一实例,比较直接用==;
     */
 
}

  

  

posted on   anpeiyong  阅读(41)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2019-06-15 JavaSE---环境配置

导航

< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5
点击右上角即可分享
微信分享提示