一、结论
- BeanUtils.copyProperties():浅拷贝
- JSONObject.parseObject():深拷贝
二、证明BeanUtils.copyProperties()是浅拷贝
代码:
import org.springframework.beans.BeanUtils;
public class Test {
public static void main(String[] args) throws Exception {
Info info1 = new Info();
Student student = new Student();
student.setName("李华");
student.setAge(18);
info1.setCountry("中国");
info1.setStudent(student);
Info info2 = new Info();
BeanUtils.copyProperties(info1, info2);
info2.setCountry("American");
info2.getStudent().setName("Tom");
info2.getStudent().setAge(18);
System.out.println(info1);
System.out.println(info2);
}
}
class Info {
/** 国家信息 **/
private String country;
/** 学生基本信息 **/
private Student student;
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@Override
public String toString() {
return "Info{" +
"country='" + country + '\'' +
", student=" + student +
'}';
}
}
/**
* 引用数据类型
*/
class Student {
/** 姓名 **/
private String name;
/** 年龄 **/
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
结果:
Info{country='中国', student=Student{name='Tom', age=18}}
Info{country='American', student=Student{name='Tom', age=18}}
解释:
从结果中可以看到student都是Tom,说明这是浅拷贝
拓展:
不太懂BeanUtils.copyProperties()
使用的请看BeanUtils.copyProperties(A, B)的使用
三、证明JSONObject.parseObject()是深拷贝
代码:
class Test {
public static void main(String[] args) throws Exception {
Info info1 = new Info();
Student student = new Student();
student.setName("李华");
student.setAge(18);
info1.setCountry("中国");
info1.setStudent(student);
Info info2 = JSONObject.parseObject(JSONObject.toJSONString(info1), new TypeReference<Info>(){});
info2.setCountry("American");
info2.getStudent().setName("Tom");
info2.getStudent().setAge(18);
System.out.println(info1);
System.out.println(info2);
}
}
class Info {
/** 国家信息 **/
private String country;
/** 学生基本信息 **/
private Student student;
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@Override
public String toString() {
return "Info{" +
"country='" + country + '\'' +
", student=" + student +
'}';
}
}
/**
* 引用数据类型
*/
class Student {
/** 姓名 **/
private String name;
/** 年龄 **/
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
结果:
Info{country='中国', student=Student{name='李华', age=18}}
Info{country='American', student=Student{name='Tom', age=18}}
解释:
从结果中可以看到student对象的名字是不一样的,说明是深拷贝
四、总结
例子中的Info类以及属性Student类都没有实现Cloneable接口,也没有重写clone()方法,说它们是浅拷贝或者深拷贝意义不大,但是道理是差不多的,如果想搞懂什么是浅拷贝,什么是深拷贝,请观看引用拷贝、浅拷贝、深拷贝