旗下导航:搜·么
当前位置:网站首页 > JAVA教程 > 正文

java中的浅拷贝与深拷贝【JAVA教程】,java,浅拷贝,深拷贝

作者:搜教程发布时间:2019-11-29分类:JAVA教程浏览:25评论:0


导读:1、什么叫Java浅拷贝?浅拷贝是会将对象的每一个属性举行顺次复制,然则当对象的属性值是援用范例时,本质复制的是其援用,当援用指向的值转变时也会随着变化。2、什么叫Ja...

1、什么叫Java浅拷贝?

浅拷贝是会将对象的每一个属性举行顺次复制,然则当对象的属性值是援用范例时,本质复制的是其援用,当援用指向的值转变时也会随着变化。

2、什么叫Java深拷贝?

深拷贝复制变量值,关于援用数据,则递归至基本范例后,再复制。深拷贝后的对象与本来的对象是完整断绝的,互不影响,对一个对象的修正并不会影响另一个对象。

相干视频教程引荐: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零基本入门

以上就是java中的浅拷贝与深拷贝的细致内容,更多请关注ki4网别的相干文章!

标签:java浅拷贝深拷贝


欢迎 发表评论: