概述
浅拷贝得到的对象跟原始对象共享数据,而深拷贝得到的是一份完全独立的对象
对于浅拷贝类说:拷贝后的对象中修改期间引用对象的属性之后,源对象的引用值也跟着修改
demo
@Data
public class Person implements Cloneable{
private int age;
private String name;
private Student student;
public Person(Integer age, String name,Student student) {
this.age = age;
this.name = name;
this.student = student;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
public Person(int age, String name, Student student) {
this.age = age;
this.name = name;
this.student = student;
}
public static void main(String[] args) throws CloneNotSupportedException {
Student student = new Student(10);
Person p = new Person(20,"zs",student);
System.out.println("p:"+p);
Person clone = (Person) p.clone();
clone.getStudent().setAge(20);
System.out.println("clone change:"+clone);
System.out.println("p change:"+p);
}
}
@Data
@AllArgsConstructor
public class Student {
private int age;
@Override
public String toString() {
return "Student{" +
"age=" + age +
'}';
}
}