代码如下(示例):
import lombok.Data; import java.util.List; @Data public class EnumInfo { private Integer code; private String remark; private List2.父类枚举类NoticeTypeEnumchildren; }
代码如下(示例):
import java.util.*; public enum NoticeTypeEnum { SYSTEM(1, "系统通知") { @Override public List3.子类枚举类MessageTypeEnumgetMessageTypes() { return Arrays.asList(MessageTypeEnum.ORDER, MessageTypeEnum.REFUND, MessageTypeEnum.AFTERSALE, MessageTypeEnum.COUPON); } }, INTERACTION(2, "互动通知") { @Override public List getMessageTypes() { return Arrays.asList(MessageTypeEnum.COMMENT, MessageTypeEnum.LIKE, MessageTypeEnum.REPLY); } }, DELIVER(3, "物流通知") { @Override public List getMessageTypes() { return Arrays.asList(MessageTypeEnum.SHIPMENTS, MessageTypeEnum.SIGNFOR); } }, ACTIVITY(4, "活动通知") { @Override public List getMessageTypes() { return Arrays.asList(MessageTypeEnum.ACTIVITY); } }; private NoticeTypeEnum(Integer code, String remark) { this.code = code; this.remark = remark; } private Integer code; private String remark; public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } public abstract List getMessageTypes(); public static NoticeTypeEnum getEnumByCode(Integer code) { if (code != null) { for (NoticeTypeEnum instance : NoticeTypeEnum.values()) { if (code.equals(instance.code)) { return instance; } } } return null; } }
代码如下(示例):
public enum MessageTypeEnum { ORDER(1, "订单提醒"), REFUND(2, "退款提醒"), AFTERSALE(4, "售后提醒"), COUPON(5, "优惠券提醒"), ACTIVITY(6, "活动通知"), COMMENT(7, "评论通知"), REPLY(8, "回复通知"), LIKE(9, "点赞通知"), SHIPMENTS(10, "发货通知"), SIGNFOR(11, "签收通知"); private Integer code; private String remark; private MessageTypeEnum(Integer code, String remark) { this.code = code; this.remark = remark; } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } }4.main方法进行测试(我这里为了省事写在了实体类当中)
代码如下(示例):
@Data public class EnumInfo { private Integer code; private String remark; private List5.返回数据children; public static void main(String[] args) { List enumInfos = new ArrayList<>(); for (int i = 1; i <= NoticeTypeEnum.values().length; i++) { EnumInfo enumInfo = new EnumInfo(); NoticeTypeEnum noticeTypeEnum = NoticeTypeEnum.getEnumByCode(i); enumInfo.setCode(noticeTypeEnum.getCode()); enumInfo.setRemark(noticeTypeEnum.getRemark()); enumInfo.setChildren(getMessageTypeEnumList(noticeTypeEnum.getMessageTypes())); enumInfos.add(enumInfo); } System.out.println("数据:" + JSON.toJSONString(enumInfos)); } private static List getMessageTypeEnumList(List messageTypeEnums) { List enumInfos = new ArrayList<>(); for (MessageTypeEnum messageTypeEnum : messageTypeEnums) { EnumInfo enumInfo = new EnumInfo(); enumInfo.setCode(messageTypeEnum.getCode()); enumInfo.setRemark(messageTypeEnum.getRemark()); enumInfos.add(enumInfo); } return enumInfos; } }
二、返回数据结构示意如下图
JSON格式数据
通过在线JSON转化以后如下
枚举树形结构在实际场景运用也是很多的,实现多层嵌套可根据该Demo进行改造
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)