编程语言
首页 > 编程语言> > Java Enum枚举 Enum类 带有构造方法的枚举 带有抽象方法的枚举

Java Enum枚举 Enum类 带有构造方法的枚举 带有抽象方法的枚举

作者:互联网

Java 枚举(enum) 是一个特殊的类,一般表示一组常量、enum 定义的枚举类默认继承了 java.lang.Enum 类

1. 基本应用

Java 枚举类使用 enum 关键字来定义,各个常量使用逗号 , 来分割

public enum Weekday {
    // 枚举 Weekday 是一个特殊的类
    // 每个枚举常量(如:SUN)都是一个Weekday的实例对象,并且都默认具备 public static final 修饰符
    SUN, MON, TUE, WED, THU, FRI, SAT
}

枚举常用方法:

public class WeekdayEnumTest {
    public static void main(String[] args) {
        // 迭代枚举
        for (Weekday weekday : Weekday.values()) {
            System.out.println(weekday.ordinal() + ": " + weekday.name());
        }

        // 当使用枚举类型成员时,直接使用枚举名称调用成员即可
        Weekday sunday = Weekday.SUN;
        System.out.println(sunday);

        Weekday mon = Weekday.valueOf("MON");
        System.out.println(mon);
    }
}

运行截图:
在这里插入图片描述

2. Enum类

enum 关键字定义的枚举类都默认继承了Enum 类(抽象类)、该类位于 java.lang下、该类具有两个 final 常量:

下为Enum类的部分源码:

public abstract class Enum<E extends Enum<E>> implements Comparable<E>, Serializable {

    // The name of this enum constant, as declared in the enum declaration.
    private final String name;

    public final String name() {
        return name;
    }
    
    public String toString() {
        return name;
    }
    
    /**
     * The ordinal of this enumeration constant (its position in the enum declaration, 
     * where the initial constant is assigned an ordinal of zero).
     */
    private final int ordinal;

    public final int ordinal() {
        return ordinal;
    }

    // Sole constructor.  Programmers cannot invoke this constructor.
    protected Enum(String name, int ordinal) {
        this.name = name;
        this.ordinal = 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);
    }
}

3. 构造方法

实现带有构造方法的枚举(项目中常使用的方式),示例:

public enum Sex {
    MALE(1, "男"),
    FEMALE(2, "女");

    public final Integer type;
    public final  String value;

    private Sex(Integer type, String value){
        this.type = type;
        this.value = value;
    }
}
public class SexEnumTest {
    public static void main(String[] args) {
        Sex man = Sex.MALE;

        System.out.println(man);            // MALE  调用默认的toString()
        System.out.println(man.name());     // MALE
        System.out.println(man.ordinal());  // 0

        System.out.println(man.type);       // 1
        System.out.println(man.value);      // 男
    }
}

4. 抽象方法

带有抽象方法的枚举、在上例的基础上添加一个抽象方法,如下:

public enum Sex {
    MALE(1, "男"){
        @Override
        public void haircut() {
            System.out.println("剃个光头吧~");
        }
    },
    FEMALE(2, "女"){
        @Override
        public void haircut() {
            System.out.println("还是留长发吧~");
        }
    };

    public final Integer type;
    public final  String value;

    private Sex(Integer type, String value){
        this.type = type;
        this.value = value;
    }

    public abstract void haircut();
}
public class SexEnumTest {
    public static void main(String[] args) {
        Sex man = Sex.MALE;

        System.out.println(man);            // MALE  调用默认的toString()
        System.out.println(man.name());     // MALE
        System.out.println(man.ordinal());  // 0

        System.out.println(man.type);       // 1
        System.out.println(man.value);      // 男

        man.haircut();                      // 剃个光头吧~
        Sex.FEMALE.haircut();               // 还是留长发吧~
    }
}

标签:name,Enum,System,枚举,println,带有,public,out
来源: https://blog.csdn.net/drizzletowne/article/details/121172931