java Object的clone方法,浅拷贝与深拷贝

来源:互联网 发布:avr 单片机 pdf 编辑:程序博客网 时间:2024/06/12 22:37

在Java中,可以使用两种方式创建一个对象:一是使用new关键字;二是使用clone方法复制一个对象。clone 方法在创建一个对象的时候分两步,首先根据对象的类型分配内存,然后复制给定对象的各个域到新对象中,从而完成一个对象的创建。这个域(类的成员变量)如果是基本类型,则拷贝的是值,如果是引用类型,则拷贝的是对象的引用。

看一下下面程序:

package CSDN;public class Main20171112_001 {public static class Student implements Cloneable{//必须实现这个接口,否则报异常!!!int id;String name;public Student(int id, String name){this.id = id;this.name = name;}//拷贝构造方法!!!public Student(Student student){this.id = student.id;this.name = student.name;}@Overrideprotected Student clone() throws CloneNotSupportedException {return (Student)super.clone();}}public static void main(String[] args) throws CloneNotSupportedException {Student stu1 = new Student(100,"Tom");Student stu2 = stu1.clone();System.out.println(stu1.name == stu2.name?"浅拷贝":"深拷贝");}}

运行结果

浅拷贝


要实现深拷贝则要重写clone方法,例如:

// 深拷贝@Overrideprotected Student clone() throws CloneNotSupportedException {Student student = (Student)super.clone();student.name = new String(name);return student;}


但是如果Student类还有个成员变量朋友,Friend类型,Friend有个String类型的名字,那如何实现深拷贝?

package CSDN;public class Main20171112_001 {public static class Student implements Cloneable{//必须实现这个接口,否则报异常!!!int id;String name;Friend friend;public Student(int id, String name,Friend friend){this.id = id;this.name = name;this.friend = friend;}// 深拷贝@Overrideprotected Student clone() throws CloneNotSupportedException {Student student = (Student)super.clone();student.name = new String(name);//引用类型要实现深拷贝student.friend = friend.clone();//引用类型要实现深拷贝return student;}}public static class Friend implements Cloneable{//必须实现这个接口{String name;public Friend(String name){this.name = name;}@Overrideprotected Friend clone() throws CloneNotSupportedException {// 深拷贝Friend f = (Friend)super.clone();f.name = new String(name);return f;}}public static void main(String[] args) throws CloneNotSupportedException {Friend friend = new Friend("Jack");Student stu1 = new Student(100,"Tom",friend);Student stu2 = stu1.clone();System.out.println("stu1 == stu2 ? :"+(stu1 == stu2));System.out.println("stu1.friend == stu2.friend ? :"+(stu1.friend == stu2.friend));System.out.println("stu1.friend.name == stu2.friend.name ? :"+(stu1.friend.name == stu2.friend.name));}}

运行结果
stu1 == stu2 ? :falsestu1.friend == stu2.friend ? :falsestu1.friend.name == stu2.friend.name ? :false

所以要对一个对象实现完全的深拷贝,就要对其各个引用类型的成员变量进行彻底的深拷贝!



参考文献

http://blog.csdn.net/zhangjg_blog/article/details/18369201


原创粉丝点击