【Java】通过枚举的key获取对应的value

public enum PayChannelEnum {

    /**
     * 支付方式
     */
    ALI_PAY("1", "支付宝"),
    CHAT_PAYMENT("2", "微信支付"),
    TRANSPORTATION_CARD("3", "交通卡"),
    PRE_CHARGE_CARD("4", "预充卡"),
    UNION_PAY("5", "银联"),
    OTHER("6", "其他自定义");

    private String type;
    private String name;

    PayChannelEnum(String type, String name) {
        this.type = type;
        this.name = name;
    }

    public String getType() {
        return type;
    }


    public String getName() {
        return name;
    }

    public static String getNameByKey(String key) {
        PayChannelEnum[] payArray = values();
        for (PayChannelEnum channelEnum : payArray) {
            if (channelEnum.getType().equals(key)) {
                return channelEnum.getName();
            }
        }
        return null;
    }
}

你可能感兴趣的:(Java基础)