Python 官方文档:入门教程 => 点击学习
场景 开发中经常遇到,把父类的属性拷贝到子类中。通常有2种方法: 1、一个一个set 2、用BeanUtils.copyProperties 很显然BeanUtils更加方便,也美观
开发中经常遇到,把父类的属性拷贝到子类中。通常有2种方法:
1、一个一个set
2、用BeanUtils.copyProperties
很显然BeanUtils更加方便,也美观很多。
那么任何情况都能使用BeanUtils么,当然不是。要先了解他。
是浅拷贝。
浅拷贝: 只是调用子对象的set方法,并没有将所有属性拷贝。(也就是说,引用的一个内存地址)
深拷贝: 将子对象的属性也拷贝过去。
如果都是单一的属性,那么不涉及到深拷贝的问题,适合用BeanUtils。
并不绝对,这个要区分考虑:
1、子对象还要改动。
2、子对象不怎么改动。
虽然有子对象,但是子对象并不怎么改动,那么用BeanUtils也是没问题的。
下面用代码说明下。
翠山有个儿子无忌,儿子继承了他的face和height。
但是life应该是自己的。
后来翠山自刎而死,无忌也变成dead状态了。这就是浅拷贝,无忌用的life引用的翠山的life对象。
Father类:
@Data
public class Father {
private String face; // 长相
private String height; // 身高
private Life life; // 生命
}
Life 类:
@Data
public class Life {
private String status;
}
Son类和main方法:
@Data
public class Son extends Father{
private Life life;
public static void main(String[] args) {
Father cuishan = new Father();
cuishan.setFace("handsome");
cuishan.setHeight("180");
Life cuishanLife = new Life();
cuishanLife.setStatus("alive");
cuishan.setLife(cuishanLife);
Son wuji=new Son();
BeanUtils.copyProperties(cuishan,wuji);
// Life wujiLife = wuji.getLife();
// wujiLife.setStatus("alive");
// wuji.setLife(wujiLife);
// cuishanLife.setStatus("dead"); // 翠山后来自刎了
System.out.println(JSON.tojsONString(cuishan));
System.out.println(JSON.toJSONString(wuji));
}
}
上面注释出的代码可以如下替换:
case1和case2还是受浅拷贝的影响,case3不受。
case1: 翠山自刎,无忌也挂了
// Life wujiLife = wuji.getLife();
// wujiLife.setStatus("alive");
// wuji.setLife(wujiLife);
// cuishanLife.setStatus("dead"); // 翠山后来自刎了
case2: 翠山自刎,无忌设置或者,翠山也活了
// cuishanLife.setStatus("dead"); // 翠山后来自刎了
// Life wujiLife = wuji.getLife();
// wujiLife.setStatus("alive");
// wuji.setLife(wujiLife);
case3: 翠山和无忌互不影响
cuishanLife.setStatus("dead"); // 翠山自刎了 该行放在上下均可
// 无忌用个新对象 不受翠山影响了
Life wujiLife = new Life();
wujiLife.setStatus("alive");
wuji.setLife(wujiLife);
笔者在这个爬过坑。
因为记得有beanutils这个工具,直接import了。
发现有copyProperty和copyProperties。 看了下发现是 dest,src ,于是果断使用,结果发现参数没了。
其实常见的BeanUtils有2个:
spring有BeanUtils
apache的commons也有BeanUtils。
区别如下:
– | spring的BeanUtils | commons的BeanUtils |
---|---|---|
方法 | copyProperty和copyProperties | copyProperties |
参数 | src ,dest | dest,src |
这2个用哪个都行,但是要注意区别。 因为他们2个的src和dest是正好相反的,要特别留意。
到此这篇关于Java BeanUtils.copyProperties的详解的文章就介绍到这了,更多相关Java BeanUtils.copyProperties内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: Java BeanUtils.copyProperties的详解
本文链接: https://www.lsjlt.com/news/132283.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0