亲宝软件园·资讯

展开

java 枚举类型 java枚举类型-Enum

Maggieq8324 人气:1
想了解java枚举类型-Enum的相关内容吗,Maggieq8324在本文为您仔细讲解java 枚举类型的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:java,Enum,java枚举类型,下面大家一起来学习吧。

前言

枚举是 Java1.5 引入的新特性,通过关键字 enum 来定义枚举类。

应用

定义

enum WeekEnum {
    Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, weekend
}

基本Enum特性

enum WeekEnum {
    Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, weekend;

    public static void main(String[] args) {
        for (WeekEnum item : WeekEnum.values()) {
            System.out.println(item.ordinal());
            System.out.println(item.name());
            System.out.println(item.getDeclaringClass());
            System.out.println(Enum.valueOf(WeekEnum.class, item.name()));
        }
    }
}

方法 说明
values() 可以遍历enum实例,其返回enum实例的数组
ordinal() 返回每个实例在声明时的次序
name() 返回enum实例声明时的名称
getDeclaringClass() 返回其所属的enum类
Enum.valueOf() 根据给定的名称返回相应的enum实例

Enum的静态导入

import static com.coisini.springbootlearn.WeekEnum.*;

public class WeekEnumTest {

    public static void main(String[] args) {
        System.out.println(Monday.name()); // Monday
        System.out.println(Tuesday.name()); //Tuesday
    }

}

Enum中添加新方法

public enum WeekEnum {

    Monday(1, "周一"),
    Tuesday(2, "周二"),
    Wednesday(3, "周三"),
    Thursday(4, "周四"),
    Friday(5, "周五"),
    Saturday(6, "周六"),
    weekend(7, "周天");

    private Integer value;
    private String description;

    /**
     * 构造方法
     * @param value
     * @param description
     */
    WeekEnum(Integer value, String description) {
        this.value = value;
        this.description = description;
    }

    /**
     * 获取value
     * @return
     */
    public Integer getValue() { return this.value; }

    /**
     * 获取Desc
     * @return
     */
    public String getDesc() { return this.description; }

    /**
     * 重写ToString方法
     * @return
     */
    public String toString() {
        return this.getValue() + " " + this.getDesc();
    }

    public static void main(String[] args) {
        for (WeekEnum item : WeekEnum.values()) {
            System.out.println(item);
        }
    }

}

Switch语句中的Enum

WeekEnum weekEnum = WeekEnum.Friday;

switch (weekEnum) {
   case Friday:
       System.out.println(weekEnum);
       break;
   case Saturday:
       break;
}

WeekEnum weekEnum = WeekEnum.Friday;

switch(weekEnum) {
 // An enum switch case label must be the unqualified name of an enumeration constant
   case WeekEnum.Friday:
       break;
   default:
       break;
}

Enum的继承

public interface Period {

    enum WeekEnum implements Period{
        Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, weekend
    }

    enum MonthEnum implements Period{
        January, February, March, April, May, June
    }
    
    class PeriodTest {
        public static void main(String[] args) {
            Period week = WeekEnum.Friday;
            Period month = MonthEnum.April;

            System.out.println(month + " " + week); // April Friday
        }
    }

}

EnumSet的使用

import java.util.EnumSet;

enum EnumSetTest {
    Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, weekend;

    public static void main(String[] args) {
        EnumSet<EnumSetTest> points = EnumSet.noneOf(EnumSetTest.class);
        points.add(Monday);
        System.out.println(points); // [Monday]

        points.addAll(EnumSet.of(Tuesday, Wednesday));
        System.out.println(points); // [Monday, Tuesday, Wednesday]

        points = EnumSet.allOf(EnumSetTest.class);
        System.out.println(points); // [Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, weekend]

        points.removeAll(EnumSet.of(Monday, Tuesday));
        System.out.println(points); // [Wednesday, Thursday, Friday, Saturday, weekend]

        points = EnumSet.complementOf(points);
        System.out.println(points); // [Monday, Tuesday]
    }
}

EnumMap的使用

import java.util.EnumMap;
import java.util.Map;

public enum EnumMapTest {
    MON, TUE, WED, THU, FRI, SAT, WEE;

    public static void main(String[] args) {
        EnumMap<EnumMapTest, String> enumMap = new EnumMap<EnumMapTest, String>(EnumMapTest.class);
        enumMap.put(MON, "Monday");
        enumMap.put(TUE, "Tuesday");

        for (Map.Entry<EnumMapTest, String> em : enumMap.entrySet()) {
            System.out.println(em.getKey() + " " + em.getValue()); // MON Monday TUE Tuesday
        }
    }
}

常量相关方法

public enum EnumAbstract {
    MON {
        void getDetail() {
            System.out.println("Monday");
        }
    },
    TUE {
        void getDetail() {
            System.out.println("Tuesday");
        }
    },
    WED {
        void getDetail() {
            System.out.println("Wednesday");
        }
    };

    abstract void getDetail();

    public static void main(String[] args) {
        for (EnumAbstract ea : EnumAbstract.values()) {
            ea.getDetail();
        }
    }

}

枚举值向枚举类型转换

import java.util.stream.Stream;

public enum WeekEnum {

    Monday(1, "周一"),
    Tuesday(2, "周二"),
    Wednesday(3, "周三"),
    Thursday(4, "周四"),
    Friday(5, "周五"),
    Saturday(6, "周六"),
    weekend(7, "周天");

    private Integer value;
    private String description;

    /**
     * 构造方法
     * @param value
     * @param description
     */
    WeekEnum(Integer value, String description) {
        this.value = value;
        this.description = description;
    }

    /**
     * 枚举值向枚举类型转换
     * @param value
     * @return
     */
    public static WeekEnum toWeekEnum(int value) {
        return Stream.of(WeekEnum.values())
                .filter(item -> item.value == value)
                .findAny()
                .orElse(null);
    }

    public static void main(String[] args) {
        System.out.println(WeekEnum.toWeekEnum(1)); // Monday
    }

}

加载全部内容

相关教程
猜你喜欢
用户评论