[英]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.