首頁  >  文章  >  Java  >  java中的淺拷貝與深拷貝

java中的淺拷貝與深拷貝

王林
王林轉載
2019-11-28 16:48:321896瀏覽

java中的淺拷貝與深拷貝

1、什麼叫Java淺拷貝?

淺拷貝是會將物件的每個屬性依序複製,但是當物件的屬性值是引用型別時,實質複製的是其引用,當引用指向的值改變時也會跟著變化。

2、什麼叫Java深拷貝?

深拷貝複製變數值,對於引用數據,則遞歸至基本型別後,再複製。深拷貝後的物件與原來的物件是完全隔離的,互不影響,對一個物件的修改並不會影響另一個物件。

相關影片教學推薦:java線上影片

3、Java淺拷貝和深拷貝的差異是什麼?

通俗來講淺拷貝的複製其引用,當引用指向的值改變時也會跟著變化;而深拷貝則是與原來的物件完全隔離,互補影響。

4、心智圖

java中的淺拷貝與深拷貝

#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零基礎入門

以上是java中的淺拷貝與深拷貝的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:csdn.net。如有侵權,請聯絡admin@php.cn刪除