深拷贝与浅拷贝

深拷贝与浅拷贝

浅拷贝:只是拷贝了源对象的地址,所以源对象的任何值发生改变了,拷贝对象的值也随之发生改变。
深拷贝:深拷贝拷贝的是源对象的所有值而不是地址,源对象发生改变了,而拷贝对象也不会发生改变。

测试:

package com.ycz.entity;

/**
 * @Description:
 * @Author: Alex
 * @Date 2022-07-03-17:08
 * @Version: V1.0
 **/
public class User {
    private String username;

    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public User() {
    }

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }
}

浅拷贝测试

package com.ycz.entity;

/**
 * @Description:
 * @Author: Alex
 * @Date 2022-07-03-17:10
 * @Version: V1.0
 **/
public class Test {
    public static void main(String[] args) {
        User user01  = new User();
        user01.setUsername("abc");
        user01.setPassword("123456");
        User user02 = user01;
        //两个对象的地址查看是否相同
        System.out.println(user01);
        System.out.println(user02);
         //修改了username,看是否有所改变
        user01.setUsername("啊实打实大师多");
        System.out.println(user01.getUsername());
        System.out.println(user02.getUsername());


    }
}

结果:
在这里插入图片描述

深拷贝测试

修改User,重写clone,实现Cloneable方法

public class User implements Cloneable{
    private String username;

    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public User() {
    }

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return (User)super.clone();
    }
}

测试:

public class Test02 {
    public static void main(String[] args) throws CloneNotSupportedException {

        User user01  = new User();
        user01.setUsername("abc");
        user01.setPassword("123456");

        User user03 = (User) user01.clone();
        System.out.println(user01);
        System.out.println(user03);

        user01.setUsername("asdasd");

        System.out.println(user03.getUsername());
    }
}

结果:
在这里插入图片描述
会发现地址都是不一样的,修改之后肯定也是不一样的

让我们来复杂一点的

修改user , 此时user重写了clone

package com.ycz.entity;

/**
 * @Description:
 * @Author: Alex
 * @Date 2022-07-03-17:08
 * @Version: V1.0
 **/
public class User implements Cloneable{
    private String username;

    private String password;

    private Peg peg;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Peg getPeg() {
        return peg;
    }

    public void setPeg(Peg peg) {
        this.peg = peg;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public User() {
    }

    public User(String username, String password , Peg peg) {
        this.username = username;
        this.password = password;
        this.peg = peg;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return (User)super.clone();
    }
}

Peg没有重写clone

package com.ycz.entity;

/**
 * @Description:
 * @Author: Alex
 * @Date 2022-07-03-17:36
 * @Version: V1.0
 **/
public class Peg implements Cloneable{
    private String name;

    private Integer age;

    public Peg(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public Peg() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

测试

package com.ycz.entity;

/**
 * @Description:
 * @Author: Alex
 * @Date 2022-07-03-17:38
 * @Version: V1.0
 **/
public class Test03 {
    public static void main(String[] args) throws CloneNotSupportedException {
        User user = new User();
        user.setUsername("abc");
        user.setPassword("123456");
        Peg peg = new Peg();
        peg.setName("小狗");
        peg.setAge(18);
        user.setPeg(peg);



        //试一下深拷贝 , peg为浅拷贝
        User user02 = (User) user.clone();
        System.out.println(user02);
        System.out.println(user);

        System.out.println(user02.getPeg());
        System.out.println(user.getPeg());
        peg.setAge(20);
        System.out.println("user-->"+user.getPeg().getAge());
        System.out.println("user02-->"+user02.getPeg().getAge());

        user.setUsername("abvasda");

        System.out.println("user-->"+user.getUsername());
        System.out.println("user02-->"+user02.getUsername());

    }

}


会发现,user是深拷贝,peg为浅拷贝
在这里插入图片描述

继续测试,这时候将peg也加上clone,且重写user里面的clone

package com.ycz.entity;

/**
 * @Description:
 * @Author: Alex
 * @Date 2022-07-03-17:36
 * @Version: V1.0
 **/
public class Peg implements Cloneable{
    private String name;

    private Integer age;

    public Peg(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public Peg() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

package com.ycz.entity;

/**
 * @Description:
 * @Author: Alex
 * @Date 2022-07-03-17:08
 * @Version: V1.0
 **/
public class User implements Cloneable{
    private String username;

    private String password;

    private Peg peg;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Peg getPeg() {
        return peg;
    }

    public void setPeg(Peg peg) {
        this.peg = peg;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public User() {
    }

    public User(String username, String password , Peg peg) {
        this.username = username;
        this.password = password;
        this.peg = peg;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        User user = (User) super.clone();
        user.setPeg((Peg) user.peg.clone());
        return user;
    }
}

测试:

package com.ycz.entity;

/**
 * @Description:
 * @Author: Alex
 * @Date 2022-07-03-17:38
 * @Version: V1.0
 **/
public class Test03 {
    public static void main(String[] args) throws CloneNotSupportedException {
        User user = new User();
        user.setUsername("abc");
        user.setPassword("123456");
        Peg peg = new Peg();
        peg.setName("小狗");
        peg.setAge(18);
        user.setPeg(peg);



        //试一下深拷贝 , peg为浅拷贝
        User user02 = (User) user.clone();
        System.out.println(user02);
        System.out.println(user);

        System.out.println(user02.getPeg());
        System.out.println(user.getPeg());
        peg.setAge(20);
        System.out.println("user-->"+user.getPeg().getAge());
        System.out.println("user02-->"+user02.getPeg().getAge());

        user.setUsername("abvasda");

        System.out.println("user-->"+user.getUsername());
        System.out.println("user02-->"+user02.getUsername());

    }

}

结果:发现都是深拷贝了
在这里插入图片描述

再我们测试一下,看都为浅拷贝的结果

package com.ycz.entity;

/**
 * @Description:
 * @Author: Alex
 * @Date 2022-07-03-17:08
 * @Version: V1.0
 **/
public class User implements Cloneable{
    private String username;

    private String password;

    private Peg peg;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Peg getPeg() {
        return peg;
    }

    public void setPeg(Peg peg) {
        this.peg = peg;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public User() {
    }

    public User(String username, String password , Peg peg) {
        this.username = username;
        this.password = password;
        this.peg = peg;
    }

}

package com.ycz.entity;

/**
 * @Description:
 * @Author: Alex
 * @Date 2022-07-03-17:36
 * @Version: V1.0
 **/
public class Peg implements Cloneable{
    private String name;

    private Integer age;

    public Peg(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public Peg() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

}

package com.ycz.entity;

/**
 * @Description:
 * @Author: Alex
 * @Date 2022-07-03-17:38
 * @Version: V1.0
 **/
public class Test03 {
    public static void main(String[] args) throws CloneNotSupportedException {
        User user = new User();
        user.setUsername("abc");
        user.setPassword("123456");
        Peg peg = new Peg();
        peg.setName("小狗");
        peg.setAge(18);
        user.setPeg(peg);



        //试一下深拷贝 , peg为浅拷贝
        User user02 = user;
        System.out.println(user02);
        System.out.println(user);

        System.out.println(user02.getPeg());
        System.out.println(user.getPeg());
        peg.setAge(20);
        System.out.println("user-->"+user.getPeg().getAge());
        System.out.println("user02-->"+user02.getPeg().getAge());

        user.setUsername("abvasda");

        System.out.println("user-->"+user.getUsername());
        System.out.println("user02-->"+user02.getUsername());

    }

}

结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值