更新時間:2022-12-01 11:56:01 來源:動力節點 瀏覽1306次
對象克隆是指創建對象的精確副本。它創建當前對象類的新實例,并使用該對象相應字段的內容來初始化其所有字段。
在 Java 中,沒有用于創建對象副本的運算符。與 C++ 不同,在 Java 中,如果我們使用賦值運算符,那么它將創建引用變量的副本而不是對象。這可以舉個例子來解釋。下面的程序演示了相同的內容。
// Java program to demonstrate that assignment operator
// only creates a new reference to same object
import java.io.*;
// A test class whose objects are cloned
class Test {
int x, y;
Test()
{
x = 10;
y = 20;
}
}
// Driver Class
class Main {
public static void main(String[] args)
{
Test ob1 = new Test();
System.out.println(ob1.x + " " + ob1.y);
// Creating a new reference variable ob2
// pointing to same address as ob1
Test ob2 = ob1;
// Any change made in ob2 will
// be reflected in ob1
ob2.x = 100;
System.out.println(ob1.x + " " + ob1.y);
System.out.println(ob2.x + " " + ob2.y);
}
}
輸出
10 20
100 20
100 20
要制作其對象副本的類必須在其中或其父類之一中具有公共克隆方法。
每個實現 clone() 的類都應該調用 super.clone() 來獲得克隆的對象引用。
該類還必須實現 java.lang.Cloneable 接口,我們要創建其對象克隆,否則當對該類的對象調用克隆方法時,它將拋出 CloneNotSupportedException。
句法:
受保護的對象克隆()拋出 CloneNotSupportedException
請注意——在下面的代碼示例中,clone() 方法確實創建了一個具有不同 hashCode 值的全新對象,這意味著它位于單獨的內存位置。但是由于Test對象c在Test2內部,原始類型實現了深拷貝,但是這個Test對象c仍然在t1和t2之間共享。為了克服這個問題,我們顯式地為對象變量 c 做了一個深拷貝,這將在后面討論。
// A Java program to demonstrate
// shallow copy using clone()
import java.util.ArrayList;
// An object reference of this class is
// contained by Test2
class Test {
int x, y;
}
// Contains a reference of Test and
// implements clone with shallow copy.
class Test2 implements Cloneable {
int a;
int b;
Test c = new Test();
public Object clone() throws CloneNotSupportedException
{
return super.clone();
}
}
// Driver class
public class Main {
public static void main(String args[])
throws CloneNotSupportedException
{
Test2 t1 = new Test2();
t1.a = 10;
t1.b = 20;
t1.c.x = 30;
t1.c.y = 40;
Test2 t2 = (Test2)t1.clone();
// Creating a copy of object t1
// and passing it to t2
t2.a = 100;
// Change in primitive type of t2 will
// not be reflected in t1 field
t2.c.x = 300;
// Change in object type field will be
// reflected in both t2 and t1(shallow copy)
System.out.println(t1.a + " " + t1.b + " " + t1.c.x + " " + t1.c.y);
System.out.println(t2.a + " " + t2.b + " " + t2.c.x
+ " " + t2.c.y);
}
}
輸出
10 20 300 40
100 20 300 40
在上面的示例中,t1.clone 返回對象 t1 的淺表副本。要獲得對象的深層副本,必須在獲得副本后在克隆方法中進行某些修改。
淺拷貝是復制對象的方法,在克隆中默認遵循。在此方法中,舊對象 X 的字段被復制到新對象 Y。在復制對象類型字段時,引用被復制到 Y,即對象 Y 將指向與 X 所指出的相同位置。如果字段值是原始類型,它復制原始類型的值。
因此,對對象 X 或 Y 中的引用對象所做的任何更改都將反映在其他對象中。
淺拷貝便宜且制作簡單。在上面的示例中,我們創建了對象的淺表副本。
如果我們想創建對象 X 的深層副本并將其放置在新對象 Y 中,則會創建任何引用對象字段的新副本,并將這些引用放置在對象 Y 中。這意味著在對象中引用的對象字段中所做的任何更改X 或 Y 將僅反映在該對象中,而不會反映在另一個對象中。在下面的示例中,我們創建了對象的深拷貝。
深拷貝復制所有字段并制作字段指向的動態分配內存的副本。當一個對象與其所引用的對象一起被復制時,就會發生深拷貝。
// A Java program to demonstrate
// deep copy using clone()
// An object reference of this
// class is contained by Test2
class Test {
int x, y;
}
// Contains a reference of Test and
// implements clone with deep copy.
class Test2 implements Cloneable {
int a, b;
Test c = new Test();
public Object clone() throws CloneNotSupportedException
{
// Assign the shallow copy to
// new reference variable t
Test2 t = (Test2)super.clone();
// Creating a deep copy for c
t.c = new Test();
t.c.x = c.x;
t.c.y = c.y;
// Create a new object for the field c
// and assign it to shallow copy obtained,
// to make it a deep copy
return t;
}
}
public class Main {
public static void main(String args[])
throws CloneNotSupportedException
{
Test2 t1 = new Test2();
t1.a = 10;
t1.b = 20;
t1.c.x = 30;
t1.c.y = 40;
Test2 t3 = (Test2)t1.clone();
t3.a = 100;
// Change in primitive type of t2 will
// not be reflected in t1 field
t3.c.x = 300;
// Change in object type field of t2 will
// not be reflected in t1(deep copy)
System.out.println(t1.a + " " + t1.b + " " + t1.c.x
+ " " + t1.c.y);
System.out.println(t3.a + " " + t3.b + " " + t3.c.x
+ " " + t3.c.y);
}
}
輸出
10 20 30 40
100 20 300 40
在上面的示例中,我們可以看到已經為 Test 類分配了一個新對象來復制一個對象,該對象將返回給 clone 方法。因此,t3 將獲得對象 t1 的深拷貝。因此,t3 對“c”對象字段所做的任何更改都不會反映在 t1 中。
如果我們使用賦值運算符將一個對象引用賦給另一個引用變量,那么它將指向舊對象的相同地址位置,并且不會創建該對象的新副本。因此,引用變量中的任何更改都將反映在原始對象中。
如果我們使用復制構造函數,那么我們必須顯式復制所有數據,即我們必須顯式地在構造函數中重新分配類的所有字段。但是在克隆方法中,創建新副本的工作是由方法本身完成的。所以為了避免額外的處理,我們使用對象克隆。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習