关于Java的浅拷贝与深拷贝

 一:浅拷贝
先创建一个对象将被拷贝对象中的每个成员变量中的值域原封不动的拷贝到新对象中
如果元对象中的成员都是内置类型的,浅拷贝没有什么危害
如果原对象中的成员包含引用类型的(PeopleMoney),浅拷贝有缺陷
源对象和新对象中的引用类型的成员变量将来指向的是同一份实体(地址相同)一个对象将该实体修改后,其他引用该实习的对象也被修改

//Cloneable 是标记接口
public class People implements Cloneable {
     String name;
     int age;
     PeopleMoney p;  
     public People() {
    }
    public People(String name, int age, PeopleMoney p) {
        this.name = name;
        this.age = age;
        this.p = p;
    }
 
    @Override
    protected Object clone() throws CloneNotSupportedException {
       return super.clone();
    }
    @Override
    public String toString() {
        return name+","+age+","+p.money;
    }
}

public class PeopleMoney {
    int money;

    public PeopleMoney() {
    }
    public PeopleMoney(int money) {
        this.money = money;
    }
}
public class PeopleTest {
public static void method() throws CloneNotSupportedException {
    People p1=new People("张三",18,new PeopleMoney(4534646));
    People p2=(People)p1.clone();
    p1.age=20;
    System.out.println(p1);
    System.out.println(p2);
    p2.age=30;
    System.out.println(p1);
    System.out.println(p2);
    p1.p.money=1463434;
    System.out.println(p1);
    System.out.println(p2);
     p2.p.money=9463431;
    System.out.println(p1);
    System.out.println(p2);

}
    public static void main(String[] args) throws CloneNotSupportedException {
        System.out.println("浅拷贝");
     method();
    }
}

 

二: 深拷贝

在clone时,如果对象中包含引用类型,就需要使用深拷贝 即:将对象的引用引用的实体再拷贝一份需要重写clone

public class Date implements Cloneable{
    int year;
    int month;
    int day;
    Time t;
    public Date() {
    }
    public Date(int year, int month, int day, Time t) {
        this.year = year;
        this.month = month;
        this.day = day;
        this.t = t;
    }
    @Override
    protected Date clone() throws CloneNotSupportedException {
        Time T=new Time(t.hours,t.minutes,t.seconds);
        return new Date(year,month,day,T);

    }
    @Override
    public String toString() {
        return year+"-"+month+"-"+day+"-"+t.hours+"-"+t.minutes+"-"+t.seconds;
    }
}

public class Time {
    int hours;
    int minutes;
    int seconds;

    public Time(int hours, int minutes, int seconds) {
        this.hours = hours;
        this.minutes = minutes;
        this.seconds = seconds;
    }
}
public class CloneTest {
    public static void method3() throws CloneNotSupportedException {
        //在clone时,如果对象中包含引用类型,就需要使用深拷贝
        //即:将对象的引用引用的实体再拷贝一份
        //需要重写clone
        Date a=new Date(2022,9,9,new Time(17,49,32));
        Date b=(Date)a.clone();
        a.day=10;
        System.out.println(a);
        System.out.println(b);
        b.t.seconds=7;
        System.out.println(a);
        System.out.println(b);
    }
    public static void main(String[] args) throws CloneNotSupportedException {
        System.out.println("深拷贝");
        method3();
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jhpan666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值