Java枚举类,这样使用优雅、易懂

Java 枚举是一个特殊的类,一般表示一组常量,比如一年的 4 个季节,一个年的 12 个月份,一个星期的 7 天,方向有东南西北等。

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

 @Test
    public void test() {
        // 1.获取改枚举值和描述
        Integer type1 = LoginTypeEnum.EMAILLOGIN.getType();
        System.out.println(type1);
        String description1 = LoginTypeEnum.EMAILLOGIN.getDescription();
        System.out.println(description1);
        // 2.获取描述
        String description = LoginTypeEnum.getDescription(1);
        System.out.println(description);
        // 3.获取枚举类
        LoginTypeEnum type = LoginTypeEnum.getType(1);
        switch (type) {
            case SCANLOGIN:
                System.out.println("扫描");
                break;
            case EMAILLOGIN:
                System.out.println("邮件");
                break;
            case MESSAGELOGIN:
                System.out.println("短信");
                break;
            default:
                System.out.println("无");
        }
    }
package com.foodie.enumeration;

import lombok.Getter;

import java.util.stream.Stream;

@Getter
public enum LoginTypeEnum {
  EMAILLOGIN(1, "邮件登陆"),
  MESSAGELOGIN(2, "短信登陆"),
  SCANLOGIN(3, "扫描登陆");
  Integer type;
  String description;

  LoginTypeEnum(Integer value, String description) {
  this.type = value;
  this.description = description;
 }

  /**
 * 通过类型获取枚举类
  *
 * @param type
  */
  public static LoginTypeEnum getType(Integer type) {
  return Stream.of(LoginTypeEnum.values()).filter(s -> s.type.equals(type)).findAny().orElse(null);
 }
  /**
 * 通过类型获取描述值
  *
 * @param type
  * @return
  */
  public static String getDescription(Integer type) {
  LoginTypeEnum[] values = LoginTypeEnum.values();
  return Stream.of(values).filter(s -> s.type == type).map(s -> s.description).findAny().orElse(null);
 }}

当为匹配行满足条件的枚举类,则会抛异常,具体解决办法。

如果 loginType 为 null,在使用 switch 语句时会抛出 NullPointerException 异常。为了避免这种情况,可以在 switch 语句之前先进行空指针判断,如下所示:

LoginType loginType = LoginType.getLoginType(30);
if (loginType != null) {
    switch (loginType) {
        case MSG:
            System.out.println("短信");
            break;
        case EMAIL:
            System.out.println("邮箱");
            break;
        case WEIXIN:
            System.out.println("微信");
            break;
        default:
            System.out.println("未获取到");
    }
} else {
    System.out.println("loginType 为 null");
}
Java
在上面的代码中,如果 loginType 为 null,将输出“loginType 为 null”这句话,而不会抛出空指针异常。
LoginType loginType = null;
if (param != null) {
    loginType = LoginType.getLoginType(param);
}
if (loginType != null) {
    switch (loginType) {
        case MSG:
            System.out.println("短信");
            break;
        case EMAIL:
            System.out.println("邮箱");
            break;
        case WEIXIN:
            System.out.println("微信");
            break;
        default:
            throw new IllegalArgumentException("不支持的 LoginType 类型:" + loginType);
    }
} else {
    throw new IllegalArgumentException("参数值不能为 null");
}

Java枚举类,这样使用优雅、易懂

本作品采用《CC 协议》,转载必须注明作者和本文链接
MissYou123
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!