在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;
}
@Override
protected 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方法,例如:
// 深拷贝
@Override
protected Student clone() throws CloneNotSupportedException {
Student student = (Student)super.clone();
student.name = new String(name);
return student;
}
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;
}
// 深拷贝
@Override
protected 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;
}
@Override
protected 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 ? :false
stu1.friend == stu2.friend ? :false
stu1.friend.name == stu2.friend.name ? :false
所以要对一个对象实现完全的深拷贝,就要对其各个引用类型的成员变量进行彻底的深拷贝!
参考文献
http://blog.csdn.net/zhangjg_blog/article/details/18369201