Java淺拷貝與深拷貝
轉載請標明出處:
https://blog.csdn.net/u011035026/article/details/103269326
1、什麼叫Java淺拷貝?
淺拷貝是會將對象的每個屬性進行依次複製,但是當對象的屬性值是引用類型時,實質複製的是其引用,當引用指向的值改變時也會跟着變化。
2、什麼叫Java深拷貝?
深拷貝複製變量值,對於引用數據,則遞歸至基本類型後,再複製。深拷貝後的對象與原來的對象是完全隔離的,互不影響,對一個對象的修改並不會影響另一個對象。
3、Java淺拷貝和深拷貝的區別是什麼?
通俗來講淺拷貝的複製其引用,當引用指向的值改變時也會跟着變化;而深拷貝則是與原來的對象完全隔離,互補影響。
4、思維導圖
5、測試用例分析
- 淺拷貝測試用例
public class ShallowExperience {
private String skill;
public void setSkill(String skill) {
this.skill = skill;
}
public void setShallowExperience(String skill) {
this.skill = skill;
}
@Override
public String toString() {
return skill;
}
}
public class ShallowCloneTest implements Cloneable {
private int age;
private ShallowExperience shallowExperience;
public ShallowCloneTest() {
this.age = 10;
this.shallowExperience = new ShallowExperience();
}
public ShallowExperience getExperience() {
return shallowExperience;
}
public void setShallowExperience(String skill) {
shallowExperience.setShallowExperience(skill);
}
public void show() {
System.out.println(shallowExperience.toString());
}
public int getAge() {
return age;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return (ShallowCloneTest) super.clone();
}
}
public class TestMain {
public static void main(String[] args) throws CloneNotSupportedException {
System.out.println("======淺拷貝======");
shallowCloneTest();
}
/**
* 淺拷貝測試用例
*
* @throws CloneNotSupportedException
*/
private static void shallowCloneTest() throws CloneNotSupportedException {
ShallowCloneTest test = new ShallowCloneTest();
test.setShallowExperience("我是小明,我精通Java,C++的複製粘貼");
test.show();
ShallowCloneTest cloneTest = (ShallowCloneTest) test.clone();
cloneTest.show();
cloneTest.setShallowExperience("我是小明的副本,我精通Java,C++");
cloneTest.show();
test.show();
System.out.println(cloneTest.getAge());
}
}
//運行結果
======淺拷貝======
我是小明,我精通Java,C++的複製粘貼
我是小明,我精通Java,C++的複製粘貼
我是小明的副本,我精通Java,C++
我是小明的副本,我精通Java,C++
10
- 深拷貝測試用例
public class DeepExperience implements Cloneable{
private String skill;
public void setSkill(String skill) {
this.skill = skill;
}
public void setDeepExperience(String skill) {
this.skill = skill;
}
@Override
public String toString() {
return skill;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class DeepCloneTest implements Cloneable {
private int age;
private DeepExperience deepExperience;
public DeepCloneTest() {
this.age = 10;
this.deepExperience = new DeepExperience();
}
public DeepExperience getExperience() {
return deepExperience;
}
public void setDeepExperience(String skill) {
deepExperience.setDeepExperience(skill);
}
public void show() {
System.out.println(deepExperience.toString());
}
public int getAge() {
return age;
}
@Override
protected Object clone() throws CloneNotSupportedException {
DeepCloneTest deepCloneTest = (DeepCloneTest) super.clone();
deepCloneTest.deepExperience = (DeepExperience) deepCloneTest.getExperience().clone();
return deepCloneTest;
}
}
public class TestMain {
public static void main(String[] args) throws CloneNotSupportedException {
System.out.println("======深拷貝======");
deepCloneTest();
}
/**
* 深拷貝測試用例
*
* @throws CloneNotSupportedException
*/
private static void deepCloneTest() throws CloneNotSupportedException {
DeepCloneTest test = new DeepCloneTest();
test.setDeepExperience("我是小明,我精通Java,C++的複製粘貼");
test.show();
DeepCloneTest cloneTest = (DeepCloneTest) test.clone();
cloneTest.show();
cloneTest.setDeepExperience("我是小明的副本,我精通Java,C++");
cloneTest.show();
test.show();
System.out.println(cloneTest.getAge());
}
}
//運行結果
======深拷貝======
我是小明,我精通Java,C++的複製粘貼
我是小明,我精通Java,C++的複製粘貼
我是小明的副本,我精通Java,C++
我是小明,我精通Java,C++的複製粘貼
10