JavaBean中属性 顺序的问题

版权声明:本文为junehappylove原创文章,未经junehappylove允许不得转载。 https://blog.csdn.net/junehappylove/article/details/83896853

在代码实践中发下这样一个有趣的问题,求大佬解释?


public class ModelData {

	@Getter @Setter
	private String name;
	@Getter @Setter
	private String desc;
	@Getter @Setter
	private String formula;// 规则:all[所有都需要满足] , least[至少一个满足], only[有且仅有一个]
	@Getter @Setter
	private List<ModelData> children;
	@Getter @Setter
	@com.fasterxml.jackson.annotation.JsonIgnore
	private Data data;

	public String convert() throws JsonProcessingException {
		data = new Data();
		data.setModel(this);
		return new ObjectMapper().writeValueAsString(data);
	}

	private class Data implements Cloneable {
		private String name;
		private String value;
		private String formula;//将其放到children前面
		private List<Data> children;
		@com.fasterxml.jackson.annotation.JsonIgnore
		private ModelData model;

		public String getName() {
			this.name = model.getName();
			return name;
		}

		public String getValue() {
			this.value = model.getDesc();
			return value;
		}

		public String getFormula() {
			this.formula = model.getFormula();
			return formula;
		}

		public List<Data> getChildren() {
			List<ModelData> list = model.getChildren();
			List<Data> temp = new ArrayList<>();
			if (list == null || list.isEmpty()) {
				return null;
			}
			for (ModelData model : list) {
				this.name = model.name;
				this.value = model.desc;
				this.formula = model.formula;
				this.model = model;
				model.data = this;
				temp.add(this.clone());
			}
			return temp;
		}

		@Override
		protected Data clone() {
			Data data = null;
			try {
				data = (Data) super.clone();
			} catch (CloneNotSupportedException e) {
				e.printStackTrace();
			}
			return data;
		}

		@Override
		public String toString() {
			return "{name:" + getName() + ",value:" + getValue() + ",formula:" + getFormula() + ",children:["
					+ getChildren() + "]" + "}";
		}

		public void setModel(ModelData model) {
			this.model = model;
		}
	}
}

测试代码:

public class BeanPropertyOrderTester {

	/**
	 * 这里测试用简单模拟,名称总是用小写字母,描述用大写字母 属性 formula [all least only]只选择这三个
	 * 测试也注意对规则测试,其他全当辅助<br>
	 * 模型的大致结构如下:<br>
	 * 
	 * @param args
	 */
	public static void main(String[] args) {

		ModelData j = new ModelData();
		j.setName("j");
		j.setDesc("J");
		j.setFormula("");

		ModelData k = new ModelData();
		k.setName("k");
		k.setDesc("K");
		k.setFormula("");

		ModelData e = new ModelData();
		e.setName("e");
		e.setDesc("E");
		e.setFormula("all");
		List<ModelData> ec = new ArrayList<>();
		ec.add(j);
		ec.add(k);
		e.setChildren(ec);

		ModelData l = new ModelData();
		l.setName("l");
		l.setDesc("L");
		l.setFormula("");

		ModelData m = new ModelData();
		m.setName("m");
		m.setDesc("M");
		m.setFormula("");

		ModelData f = new ModelData();
		f.setName("f");
		f.setDesc("F");
		f.setFormula("least");
		List<ModelData> fc = new ArrayList<>();
		fc.add(l);
		fc.add(m);
		f.setChildren(fc);

		ModelData n = new ModelData();
		n.setName("n");
		n.setDesc("N");
		n.setFormula("");

		ModelData g = new ModelData();
		g.setName("g");
		g.setDesc("G");
		g.setFormula("only");
		List<ModelData> gc = new ArrayList<>();
		gc.add(n);
		g.setChildren(gc);

		ModelData b = new ModelData();
		b.setName("b");
		b.setDesc("B");
		b.setFormula("least");
		List<ModelData> bc = new ArrayList<>();
		bc.add(e);
		bc.add(f);
		bc.add(g);
		b.setChildren(bc);

		ModelData h = new ModelData();
		h.setName("h");
		h.setDesc("H");
		h.setFormula("");

		ModelData i = new ModelData();
		i.setName("i");
		i.setDesc("I");
		i.setFormula("");

		ModelData c = new ModelData();
		c.setName("c");
		c.setDesc("C");
		c.setFormula("only");
		List<ModelData> cc = new ArrayList<>();
		cc.add(h);
		cc.add(i);
		c.setChildren(cc);

		ModelData d = new ModelData();
		d.setName("d");
		d.setDesc("D");
		d.setFormula("");

		ModelData a = new ModelData();
		a.setName("a");
		a.setDesc("A");
		a.setFormula("all");
		List<ModelData> ac = new ArrayList<>();
		ac.add(b);
		ac.add(c);
		ac.add(d);
		a.setChildren(ac);

		String json = null;
		try {
			json = a.convert();
		} catch (JsonProcessingException e1) {
			e1.printStackTrace();
		}
		System.out.println(json);
	}
}

说明:
上面代码中@Getter@Setter提供属性的get和set方法,转Json方法是使用jackson工具包中的方法;

功能代码说明:

后台有一个模型数据(ModelData),要展示给前台,但是某些字段项需要经过转换后才能给前台,代码中数据我已经将模型简化
模型对象是:ModelData,给前台的对象是Data
ModelData中包涵的属性有 name,desc,formula,和children;而前台Data中的属性是 name,value,formulachildren,显然定义的属性名称有些是不一样
(不要纠结为啥前后台定义的属性不统一)
这里要讨论的是一个bean中属性顺序的问题.
这里还有个注意的问题就是,在ModelData中包涵一个Data属性,在Data中包涵一个ModelData属性
这样做的目的: 为了在将ModelData转换称Data时候,对children的简便处理,否则的话,对children的处理感觉会巨麻烦(自己能力有限,欢迎大佬指正),就是children里面各个对象下还有children,无限层级的

这里需要注意了Data里面的属性顺序:在Data里面 属性children在所有的字段中是最后的,运行一遍测试:可以看出结果完美呈现,如下:

{
	"name": "a",
	"value": "A",
	"formula": "all",
	"children": [{
			"name": "b",
			"value": "B",
			"formula": "least",
			"children": [{
					"name": "e",
					"value": "E",
					"formula": "all",
					"children": [{
							"name": "j",
							"value": "J",
							"formula": "",
							"children": null
						}, {
							"name": "k",
							"value": "K",
							"formula": "",
							"children": null
						}
					]
				}, {
					"name": "f",
					"value": "F",
					"formula": "least",
					"children": [{
							"name": "l",
							"value": "L",
							"formula": "",
							"children": null
						}, {
							"name": "m",
							"value": "M",
							"formula": "",
							"children": null
						}
					]
				}, {
					"name": "g",
					"value": "G",
					"formula": "only",
					"children": [{
							"name": "n",
							"value": "N",
							"formula": "",
							"children": null
						}
					]
				}
			]
		}, {
			"name": "c",
			"value": "C",
			"formula": "only",
			"children": [{
					"name": "h",
					"value": "H",
					"formula": "",
					"children": null
				}, {
					"name": "i",
					"value": "I",
					"formula": "",
					"children": null
				}
			]
		}, {
			"name": "d",
			"value": "D",
			"formula": "",
			"children": null
		}
	]
}

上面结果正是我说需要的要展示给前台的数据;
然而,这里调一下属性的位置,在Data中,将formula字段位置放到属性children的后面,

...
private class Data implements Cloneable {
		private String name;
		private String value;
		private List<Data> children;
		private String formula;//将其放到children后面
		@com.fasterxml.jackson.annotation.JsonIgnore
		private ModelData model;
		...// 后面都一样

再运行一遍测试:就会发现farmula的数据大部分没有了,如下:

{
	"name": "a",
	"value": "A",
	"children": [{
			"name": "b",
			"value": "B",
			"children": [{
					"name": "e",
					"value": "E",
					"children": [{
							"name": "j",
							"value": "J",
							"children": null,
							"formula": ""
						}, {
							"name": "k",
							"value": "K",
							"children": null,
							"formula": ""
						}
					],
					"formula": ""
				}, {
					"name": "f",
					"value": "F",
					"children": [{
							"name": "l",
							"value": "L",
							"children": null,
							"formula": ""
						}, {
							"name": "m",
							"value": "M",
							"children": null,
							"formula": ""
						}
					],
					"formula": ""
				}, {
					"name": "g",
					"value": "G",
					"children": [{
							"name": "n",
							"value": "N",
							"children": null,
							"formula": ""
						}
					],
					"formula": ""
				}
			],
			"formula": "only"
		}, {
			"name": "c",
			"value": "C",
			"children": [{
					"name": "h",
					"value": "H",
					"children": null,
					"formula": ""
				}, {
					"name": "i",
					"value": "I",
					"children": null,
					"formula": ""
				}
			],
			"formula": ""
		}, {
			"name": "d",
			"value": "D",
			"children": null,
			"formula": ""
		}
	],
	"formula": ""
}

仅仅调了一下属性的位置啊,这是为什么呢?求大佬解答,欢迎评论,或加微信交流:junehappylove

猜你喜欢

转载自blog.csdn.net/junehappylove/article/details/83896853
今日推荐