您好,登錄后才能下訂單哦!
java中的淺拷貝和深拷貝是什么?二者有什么區(qū)別?這些問題可能是我們?nèi)粘9ぷ鲿?huì)見到的。通過這些問題,希望你能收獲更多。下面是揭開這些問題的詳細(xì)內(nèi)容。
1、什么叫Java淺拷貝?
淺拷貝是會(huì)將對(duì)象的每個(gè)屬性進(jìn)行依次復(fù)制,但是當(dāng)對(duì)象的屬性值是引用類型時(shí),實(shí)質(zhì)復(fù)制的是其引用,當(dāng)引用指向的值改變時(shí)也會(huì)跟著變化。
2、什么叫Java深拷貝?
深拷貝復(fù)制變量值,對(duì)于引用數(shù)據(jù),則遞歸至基本類型后,再復(fù)制。深拷貝后的對(duì)象與原來的對(duì)象是完全隔離的,互不影響,對(duì)一個(gè)對(duì)象的修改并不會(huì)影響另一個(gè)對(duì)象。
3、Java淺拷貝和深拷貝的區(qū)別是什么?
通俗來講淺拷貝的復(fù)制其引用,當(dāng)引用指向的值改變時(shí)也會(huì)跟著變化;而深拷貝則是與原來的對(duì)象完全隔離,互補(bǔ)影響。
4、思維導(dǎo)圖
5、測(cè)試用例分析
淺拷貝測(cè)試用例
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(); } /** * 淺拷貝測(cè)試用例 * * @throws CloneNotSupportedException */ private static void shallowCloneTest() throws CloneNotSupportedException { ShallowCloneTest test = new ShallowCloneTest(); test.setShallowExperience("我是小明,我精通Java,C++的復(fù)制粘貼"); test.show(); ShallowCloneTest cloneTest = (ShallowCloneTest) test.clone(); cloneTest.show(); cloneTest.setShallowExperience("我是小明的副本,我精通Java,C++"); cloneTest.show(); test.show(); System.out.println(cloneTest.getAge()); } } //運(yùn)行結(jié)果 ======淺拷貝====== 我是小明,我精通Java,C++的復(fù)制粘貼 我是小明,我精通Java,C++的復(fù)制粘貼 我是小明的副本,我精通Java,C++ 我是小明的副本,我精通Java,C++ 10
深拷貝測(cè)試用例
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(); } /** * 深拷貝測(cè)試用例 * * @throws CloneNotSupportedException */ private static void deepCloneTest() throws CloneNotSupportedException { DeepCloneTest test = new DeepCloneTest(); test.setDeepExperience("我是小明,我精通Java,C++的復(fù)制粘貼"); test.show(); DeepCloneTest cloneTest = (DeepCloneTest) test.clone(); cloneTest.show(); cloneTest.setDeepExperience("我是小明的副本,我精通Java,C++"); cloneTest.show(); test.show(); System.out.println(cloneTest.getAge()); } } //運(yùn)行結(jié)果 ======深拷貝====== 我是小明,我精通Java,C++的復(fù)制粘貼 我是小明,我精通Java,C++的復(fù)制粘貼 我是小明的副本,我精通Java,C++ 我是小明,我精通Java,C++的復(fù)制粘貼 10
到此為止, 關(guān)于java中的淺拷貝和深拷貝有了一個(gè)基礎(chǔ)的認(rèn)識(shí), 但是對(duì)于具體的使用方法還是需要多加鞏固和練習(xí),如果想了解更多相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。