package com.imooc.enums; public interface CodeEnum { Integer getCode(); }
package com.imooc.enums; import lombok.Getter; @Getter public enum PayStatusEnum implements CodeEnum { WAIT(0, "等待支付"), SUCCESS(1, "支付成功"), ; private Integer code; private String message; PayStatusEnum(Integer code, String message) { this.code = code; this.message = message; } }
package com.imooc.utils; import com.imooc.enums.CodeEnum; public class EnumUtil { public static <T extends CodeEnum> T getByCode(Integer code, Class<T> enumClass) { for (T each: enumClass.getEnumConstants()) { if (code.equals(each.getCode())) { return each; } } return null; } }