初始JAVA中浅拷贝和深拷贝
1. 简单变量的复制
public static void main(String[] args) { int a = 5; int b = a; System.out.println(a); System.out.println(b); }
八种基本数据类型的(int,boolean,char,byte,short,float,double.long)都适用于这种情况。
2. 对象的复制
对象的复制并没有基本类型变量的复制这么简单,下面看一个例子
2.1 准备一个Student类
package com.jepson.java; /** * @author jepson * @create 2018-09-21 23:20 */ public class Student { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
2.2 测试Object的测试
package com.jepson.java; /** * @author jepson * @create 2018-09-21 23:21 */ public class TestObjectCopy { public static void main(String[] args) { Student stu1 = new Student(); stu1.setName("jepson"); Student stu2 = stu1; System.out.println("student1:"+stu1.getName()); System.out.println("student2:"+stu2.getName()); } }
2.3 运行结果
student1:jepson
student2:jepson
2.4 总结
这里我们自定义了一个学生类,该类只有一个name字段。
我们新建了一个学生实例,然后将该值赋值给stu2实例。(Student stu2 = stu1;)
再看看打印结果,作为一个新手,拍了拍胸腹,对象复制不过如此,
难道真的是这样吗?
2.5 举一反三
我们试着改变stu2实例的name字段,再打印结果看看
package com.jepson.java; /** * @author jepson * @create 2018-09-21 23:21 */ public class TestObjectCopy { public static void main(String[] args) { Student stu1 = new Student(); stu1.setName("jepson"); Student stu2 = stu1; stu2.setName("tom"); System.out.println("student1:"+stu1.getName()); System.out.println("student2:"+stu2.getName()); } }
打印结果
student1:tom
student2:tom
这就怪了,为什么改变学生2的学号,学生1的学号也发生了变化呢?
原因出在(stu2 = stu1) 这一句。该语句的作用是将stu1的引用赋值给stu2,
这样,stu1和stu2指向内存堆中同一个对象。如图:
那么,怎样才能达到复制一个对象呢?
是否记得万类之王Object。它有11个方法,有两个protected的方法,其中一个为clone方法。
该方法的签名是:
protected native Object clone() throws CloneNotSupportedException;
因为每个类直接或间接的父类都是Object,因此它们都含有clone()方法,但是因为该方法是protected,所以都不能在类外进行访问。
要想对一个对象进行复制,就需要对clone方法覆盖。
3 对象的浅拷贝(浅复制)
1. 被复制的类需要实现Clonenable接口(不实现的话在调用clone方法会抛出CloneNotSupportedException异常) 该接口为标记接口(不含任何方法)
2. 覆盖clone()方法,访问修饰符设为public。方法中调用super.clone()方法得到需要的复制对象,(native为本地方法)
3.1 修改Student类
1.实现Cloneable接口
2.重写clone方法
3.访问修饰符改为public
package com.jepson.java; /** * @author jepson * @create 2018-09-21 23:20 */ public class Student implements Cloneable{ @Override public Object clone() { Student stu = null; try{ stu = (Student)super.clone(); }catch(CloneNotSupportedException e) { e.printStackTrace(); } return stu; } private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
3.2 修改测试类
1.调用clone方法。
package com.jepson.java; /** * @author jepson * @create 2018-09-21 23:21 */ public class TestObjectCopy { public static void main(String[] args) { Student stu1 = new Student(); stu1.setName("jepson"); Student stu2 = (Student)stu1.clone(); stu2.setName("浅拷贝-tom"); System.out.println("student1:"+stu1.getName()); System.out.println("student2:"+stu2.getName()); } }
打印结果
如果你还不相信这两个对象不是同一个对象,那么可以测试一下
package com.jepson.java; /** * @author jepson * @create 2018-09-21 23:21 */ public class TestObjectCopy { public static void main(String[] args) { Student stu1 = new Student(); stu1.setName("jepson"); Student stu2 = (Student)stu1.clone(); stu2.setName("浅拷贝-tom"); System.out.println("student1:"+stu1.getName()); System.out.println("student2:"+stu2.getName()); System.out.println(stu1==stu2); } }
打印结果
4 对象的深拷贝(深复制)
我们在学生类里再加一个Address类。
Address类如下:
package com.jepson.java; /** * @author jepson * @create 2018-09-21 23:57 */ public class Address { private String address; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Address{" + "address='" + address + '\'' + '}'; } }
Student类如下:
package com.jepson.java; /** * @author jepson * @create 2018-09-21 23:20 */ public class Student implements Cloneable{ private String name; private Address address; public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public Object clone() { Student stu = null; try{ stu = (Student)super.clone(); }catch(CloneNotSupportedException e) { e.printStackTrace(); } return stu; } }
测试类
package com.jepson.java; /** * @author jepson * @create 2018-09-21 23:21 */ public class TestObjectCopy { public static void main(String[] args) { Address address = new Address(); address.setAddress("成都市"); Student stu1 = new Student(); stu1.setName("jepson"); stu1.setAddress(address); Student stu2 = (Student)stu1.clone(); stu2.setName("深拷贝-tom"); System.out.println("student1:"+stu1.getName()+"::::"+stu1.getAddress()); System.out.println("student2:"+stu2.getName()+"::::"+stu2.getAddress()); System.out.println(stu1==stu2); } }
打印结果
乍一看没什么问题,真的是这样吗?
我们在main方法中试着改变addr实例的地址。
package com.jepson.java; /** * @author jepson * @create 2018-09-21 23:21 */ public class TestObjectCopy { public static void main(String[] args) { Address address = new Address(); address.setAddress("成都市"); Student stu1 = new Student(); stu1.setName("jepson"); stu1.setAddress(address); Student stu2 = (Student)stu1.clone(); stu2.setName("深拷贝-tom"); System.out.println("student1:"+stu1.getName()+"::::"+stu1.getAddress()); System.out.println("student2:"+stu2.getName()+"::::"+stu2.getAddress()); address.setAddress("重庆区"); stu2.setName("深拷贝-tom"); System.out.println("student1:"+stu1.getName()+"::::"+stu1.getAddress()); System.out.println("student2:"+stu2.getName()+"::::"+stu2.getAddress()); System.out.println(stu1==stu2); } }
打印结果
这就奇怪了,怎么两个学生的地址都改变了?
原因是浅复制只是复制了addr变量的引用,并没有真正的开辟另一块空间,将值复制后再将引用返回给新对象。
所以,为了达到真正的复制对象,而不是纯粹引用复制。我们需要将Address类可复制化,并且修改clone方法,完整代码如下:
1.修改Address类
1.实现Cloneable接口
2.重写clone方法
package com.jepson.java; /** * @author jepson * @create 2018-09-21 23:57 */ public class Address implements Cloneable{ private String address; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Address{" + "address='" + address + '\'' + '}'; } @Override protected Object clone() { Address addr = null; try{ addr = (Address)super.clone(); }catch(CloneNotSupportedException e) { e.printStackTrace(); } return addr; } }
2. 修改Student类
clone方法中加入深度复制
package com.jepson.java; /** * @author jepson * @create 2018-09-21 23:20 */ public class Student implements Cloneable{ private String name; private Address address; public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public Object clone() { Student stu = null; try{ stu = (Student)super.clone(); // 浅复制 }catch(CloneNotSupportedException e) { e.printStackTrace(); } stu.address = (Address)address.clone(); //深度复制 return stu; } }
3. 测试类
package com.jepson.java; /** * @author jepson * @create 2018-09-21 23:21 */ public class TestObjectCopy { public static void main(String[] args) { Address address = new Address(); address.setAddress("成都市"); Student stu1 = new Student(); stu1.setName("jepson"); stu1.setAddress(address); Student stu2 = (Student)stu1.clone(); stu2.setName("深拷贝-tom"); System.out.println("student1:"+stu1.getName()+"::::"+stu1.getAddress()); System.out.println("student2:"+stu2.getName()+"::::"+stu2.getAddress()); address.setAddress("重庆区"); stu2.setName("深拷贝-tom"); System.out.println("student1:"+stu1.getName()+"::::"+stu1.getAddress()); System.out.println("student2:"+stu2.getName()+"::::"+stu2.getAddress()); System.out.println(stu1==stu2); } }
这样结果就符合我们的想法了。
总结:浅拷贝是指在拷贝对象时,对于基本数据类型的变量会重新复制一份,而对于引用类型的变量只是对引用进行拷贝,
没有对引用指向的对象进行拷贝。
而深拷贝是指在拷贝对象时,同时会对引用指向的对象进行拷贝。
区别就在于是否对 对象中的引用变量所指向的对象进行拷贝。