java中的淺拷貝和深拷貝是什么?二者有什么區(qū)別?這些問(wèn)題可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到的。通過(guò)這些問(wèn)題,希望你能收獲更多。下面是揭開(kāi)這些問(wèn)題的詳細(xì)內(nèi)容。
合水ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書(shū)未來(lái)市場(chǎng)廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書(shū)銷(xiāo)售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書(shū)合作)期待與您的合作!
1、什么叫Java淺拷貝?
淺拷貝是會(huì)將對(duì)象的每個(gè)屬性進(jìn)行依次復(fù)制,但是當(dāng)對(duì)象的屬性值是引用類(lèi)型時(shí),實(shí)質(zhì)復(fù)制的是其引用,當(dāng)引用指向的值改變時(shí)也會(huì)跟著變化。
2、什么叫Java深拷貝?
深拷貝復(fù)制變量值,對(duì)于引用數(shù)據(jù),則遞歸至基本類(lèi)型后,再?gòu)?fù)制。深拷貝后的對(duì)象與原來(lái)的對(duì)象是完全隔離的,互不影響,對(duì)一個(gè)對(duì)象的修改并不會(huì)影響另一個(gè)對(duì)象。
3、Java淺拷貝和深拷貝的區(qū)別是什么?
通俗來(lái)講淺拷貝的復(fù)制其引用,當(dāng)引用指向的值改變時(shí)也會(huì)跟著變化;而深拷貝則是與原來(lái)的對(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)注創(chuàng)新互聯(lián)行業(yè)資訊。