浅谈浅拷贝与深拷贝

一、概念

1、浅拷贝:对基本数据类型进行值传递,对引用数据类型进行引用传递般的拷贝,此为浅拷贝。

2、深拷贝:对基本数据类型进行值传递,对引用数据类型,创建一个新的对象,并复制其内容,此为深拷贝。

二、图解
(1)浅拷贝:
在这里插入图片描述

(2)深拷贝:
在这里插入图片描述

三、实例
public class mytest {
public static void main(String[] args) {
Parent item1 = new Parent(“john”, 10);
Parent item2 = item1.clone();

    System.out.println("parent1 = " + item1.toString());  
    System.out.println("parent2 = " + item2.toString());  
}  
  
public static class Parent implements Cloneable{  
    String name = "";  
    int age = 0;  
    Parent (String n, int age){  
        this.name = n;  
        this.age = age;  
    }  

    public void setName(String na) {  
        name = na;  
    }  
    @Override  
    protected Parent clone() {  
        Parent clone = null;  
        try {  
            clone = (Parent) super.clone();  
        } catch (CloneNotSupportedException e){  
            throw new RuntimeException(e); // won't happen  
        }  
          
        return clone;  
     }  
      
    public String toString() {  
        return "Parent[" + name + "===" + age + "];";  
    }  
}  

}

运行结果:
parent1 = Parent[john=10];
parent2 = Parent[john
=10];
Parent1被复制了一份。

添加一个内部类son,并加到parent里头去。

[java] view plain copy
public class mytest {
。。。。。。。。
public static class Parent implements Cloneable{
String name = “”;
int age = 0;
Son theson;
Parent (String n, int age){
this.name = n;
this.age = age;
}

    public void setName(String na) {  
        name = na;  
    }  
      
    public void setSon(Son s) {  
        theson = s;  
    }  
    @Override  
    protected Parent clone() {  
        Parent clone = null;  
        try {  
            clone = (Parent) super.clone();  
        } catch (CloneNotSupportedException e){  
            throw new RuntimeException(e); // won't happen  
        }  
          
        return clone;  
     }  
      
    public String toString() {  
        return "Parent[" + name + "===" + age + "];" + "---Son:"+ (theson != null ? theson.name : "null");  
    }  
}  
public static class Son implements Cloneable {  
    String name = "";  
    int age = 0;  
    Son (String n, int age){  
        this.name = n;  
        this.age = age;  
    }  

    public void setName(String na) {  
        name = na;  
    }  
    @Override  
    protected Son clone() {  
        Son clone = null;  
        try {  
            clone = (Son) super.clone();  
        } catch (CloneNotSupportedException e){  
            throw new RuntimeException(e); // won't happen  
        }  
          
        return clone;  
     }  
      
    public String toString() {  
        return "Son[" + name + "===" + age + "];";  
    }  
}  

}

测试结果:

parent1与parent2中的son都变了。

可见,浅拷贝只是将内部对象的地址保存

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值