[英]How to shallow clone and deep clone in java?
我在包pack1
有三个类。 这三个类是classA
classB
和classC
。
classA
public class Address {
public String town = null;
public String street = null;
public int postCode = 0;
public int houseNumber = 0;
}
classB
public class Course {
public String number;
public String name;
public Course(){
number = null;
name = null;
}
classC
public class Student {
public Date dob;
public Course course = new Course();
public Address address = new Address();
public Student(){
dob = null;
course.name = null;
course.number = null;
address.town = null;
address.street = null;
address.postCode = 0;
address.houseNumber = 0;
course.name = null;
course.number = null;
}
我想知道我如何深度克隆地址和 dob 以及浅层克隆课程? 克隆的连击我还不知道
ShallowCopy:对象的浅拷贝将具有原始对象所有字段的精确副本。 如果原始对象有对其他对象的任何引用作为字段,那么只有这些对象的引用会被复制到克隆对象中,不会创建这些对象的副本。
深拷贝:一个对象的深拷贝将像浅拷贝一样精确复制原始对象的所有字段。 但另外,如果原始对象有任何对其他对象的引用作为字段,那么这些对象的副本也会通过对它们调用 clone() 方法来创建
由于课程实体没有任何对象引用,因此它使用默认克隆方法进行克隆。 Student Entity 作为 Date 、 Course 、 Address 的引用,我们需要重写 clone 方法。 以下是示例代码:-
public static class Address implements Cloneable{
public String town = null;
public String street = null;
public int postCode = 0;
public int houseNumber = 0;
public Address(String town , String street ,int postCode , int houseNumber){
this.town = town;
this.street = street;
this.postCode = postCode;
this.houseNumber = houseNumber;
}
public Address(){
}
//Default version of clone() method. It creates shallow copy of an object.
protected Object clone() throws CloneNotSupportedException
{
return super.clone();
}
}
public static class Course implements Cloneable{
public String number;
public String name;
public Course(){
number = null;
name = null;
}
public Course(String number , String name){
this.number = number;
this.name = name;
}
//Default version of clone() method. It creates shallow copy of an object.
protected Object clone() throws CloneNotSupportedException
{
return super.clone();
}
}
public static class Student implements Cloneable{
public Date dob;
public Course course = new Course();
public Address address = new Address();
public Student(){
dob = null;
course.name = null;
course.number = null;
address.town = null;
address.street = null;
address.postCode = 0;
address.houseNumber = 0;
}
public Student(Date dob , Course course , Address address){
this.dob = dob;
this.course = course;
this.address = address;
}
protected Object clone() throws CloneNotSupportedException
{
Student student = (Student) super.clone();
student.course = (Course) course.clone();
student.address = (Address) address.clone();
student.dob = (Date) dob.clone();
return student;
}
}
要进行浅拷贝,您只需将一个变量设置为等于另一个变量。 浅拷贝意味着原始和副本实际上是同一个对象。 如果你有:
course2 = course1;
course2.name = "math";
那么course1
的名字也会变成“math”,因为 course1 和 course2 是同一个对象。
要进行深度克隆,您必须复制对象中的所有内容,而不是对象本身。
course2 = new Course();
course2.name = course1.name;
//...
course2.name = "math";
在这种情况下, course1 的名称不会更改,因为course2
本身是作为一个新对象创建的,并且没有设置为course1
。
在ShallowCopy 中只复制引用值,因此如果对其进行任何更改,将直接影响该对象的原始副本。
但是在DeepCopy 中,您必须创建该对象的新实例并使用原始对象的值对其进行初始化,然后返回新初始化的对象。
您必须在 clone 方法中执行此操作。 这样当 clone 被调用时,deepcopy 就会被执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.