0
点赞
收藏
分享

微信扫一扫

开源模型应用落地-安全合规篇(一)

西曲风 02-24 14:00 阅读 4

1、场景一

  • 调用:OperatorType.USER.getType()
public enum OperatorType {

    USER("1"),

    ROLE("2"),

    CUSTOMER("3");

    private final String type;

    OperatorType(String type) {
        this.type = type;
    }

    public String getType() {
        return type;
    }

    public static OperatorType enumOf(String type) {
        for (OperatorType operatorType : values()) {
            if (operatorType.getType().equals(type)) {
                return operatorType;
            }
        }
        return null;
    }
}

2、场景二

public enum EnableCheck {

    DISABLE,

    ENABLE;

    public String getValue() {
        return String.valueOf(this.ordinal());
    }
}
  • ordinal() 方法是Enum类中的一个方法,用于返回枚举常量的序数,即它在枚举声明中的位置索引。默认情况下,第一个枚举常量的序数为 0,第二个为 1。

  • this.ordinal() 返回当前枚举常量的序数,然后通过 String.valueOf() 方法将其转换为字符串。getValue() 方法返回的是当前枚举常量在声明中的位置索引的字符串表示。

举报

相关推荐

0 条评论