您好,登錄后才能下訂單哦!
java中的淺拷貝和深拷貝是什么?二者有什么區別?這些問題可能是我們日常工作會見到的。通過這些問題,希望你能收獲更多。下面是揭開這些問題的詳細內容。
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
到此為止, 關于java中的淺拷貝和深拷貝有了一個基礎的認識, 但是對于具體的使用方法還是需要多加鞏固和練習,如果想了解更多相關內容,請關注億速云行業資訊。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。