对深拷贝与浅拷贝区别的认识

java中深拷贝与浅拷贝的区别

1.定义
一听到拷贝二字,很容易想到一个单词“copy”,对,就是复制的意思。那么它的作用也就是为了方便嘛。如果从字面意思上来说,深拷贝它拷贝的程度或者说级别深一点,而浅拷贝就比较浅一些。具体来说浅拷贝能复制变量,如果对象内还有对象,则只能复制对象的地址;而深拷贝能复制变量,也能复制当前对象的内部对象。注意,浅拷贝需要实现 Cloneable接口。下面用代码再进行进一步的分析。
人家说的更详细

浅拷贝是指拷贝对象时仅仅拷贝对象本身(包括对象中的基本变量),而不拷贝对象包含的引用指向的对象。深拷贝不仅拷贝对象本身,而且拷贝对象包含的引用指向的所有对象。举例来说更加清楚:对象A1中包含对B1的引用,B1中包含对C1的引用。浅拷贝A1得到A2,A2
中依然包含对B1的引用,B1中依然包含对C1的引用。深拷贝则是对浅拷贝的递归,深拷贝A1得到A2,A2中包含对B2(B1的copy)的引用,B2
中包含对C2(C1的copy)的引用。

2.代码
① 浅拷贝:
class Teacher implements Cloneable {
String name;
int age;

Teacher(String name, int age) {
this.name = name;
this.age = age;
}

public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}

class Student implements Cloneable {
String name;// 常量对象。
int age;
Teacher t;// 学生1和学生2的引用值都是一样的。

Student(String name, int age, Teacher t) {
this.name = name;
this.age = age;
this.t = t;
}

public Object clone() {
Student0 o = null;
try {
o = (Student) super.clone();
} catch (CloneNotSupportedException e) {
System.out.println(e.toString());
}

return o;
}
}

public class CopyTest {
public static void main(String[] args) {
Teacher t = new Teacher(“wangwu”, 50);
Student s1 = new Student0(“zhangsan”, 18, p);
Student s2 = (Student) s1.clone();
s2.t.name = “lisi”;
s2.t.age = 30;
s2.name = “z”;
s2.age = 45;
System.out.println(“学生s1的姓名:” + s1.name + “\n学生s1教授的姓名:” + s1.t.name + “,” + “\n学生s1教授的年纪” + s1.t.age);// 学生1的教授
}
}
可以明显的看出,s和s1都变了,相对应的t的属性也变了,说明s和是s1指向的是同一个对象。

  • 下来看一下深拷贝的案例

class Teacherimplements Cloneable {
String name;
int age;

Teacher(String name, int age) {
this.name = name;
this.age = age;
}
public Object clone() {
Object o = null;
try {
o = super.clone();
} catch (CloneNotSupportedException e) {
System.out.println(e.toString());
}
return o;
}
}

class Student implements Cloneable {
String name;
int age;
Teacher t;
Student(String name, int age,Teacher t) {
this.name = name;
this.age = age;
this.t = t;
}

public Object clone() {
Student o = null;
try {
o = (Student) super.clone();
} catch (CloneNotSupportedException e) {
System.out.println(e.toString());
}
o.p = (Teacher ) p.clone();
return o;
}
}

public class DeepCopyTest{
public static void main(String args[]) {
long t1 = System.currentTimeMillis();
Teacher t = new Teacher (“wangwu”, 50);
Student s1 = new Student(“zhangsan”, 18, p);
Student s2 = (Student) s1.clone();
s2.t.name = “lisi”;
s2.t.age = 30;
System.out.println(“name=” + s1.t.name + “,” + “age=” + s1.t.age);// 学生1的教授不改变。
long t2 = System.currentTimeMillis();
System.out.println(t2-t1);
}
}

  • 综上所述,浅拷贝是只对指针进行拷贝,两个指针指向同一个内存块,深拷贝是对指针和指针指向的内容都进行拷贝,拷贝后的指针是指向不同内的指针。

具体的内容参考博客老前辈href=“https://www.cnblogs.com/shuaiwhu/archive/2010/12/14/2065088.html”

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值