您的位置:首页 > 编程语言 > Java开发

Java对象clone

2015-09-10 18:02 399 查看
转载自:http://blog.csdn.net/jdluojing/article/details/6963112

Java里的clone分为:

      浅复制(浅克隆)
: 浅复制仅仅复制所考虑的对象,而不复制它所引用的对象。

      深复制(深克隆): 深复制把要复制的对象所引用的对象都复制了一遍。

Java中对象的克隆,为了获取对象的一份拷贝,可以利用Object类的clone()方法。必须要遵循下面三点:

    1. 在派生类中覆盖基类的clone()方法,并声明为public。(Object类中的clone()方法为protected)

    2. 在派生类的clone()方法中,需要调用super.clone()。

    3. 在派生类中实现Cloneable接口。

      Object类的clone()一个native方法,native方法的效率一般来说都是远高于java中的非native方法。这也解释了为什么要用Object中clone()方法而不是先new一个类,然后把原始对象中的信息赋到新对象中。

浅克隆

Object类里的clone()方法是浅克隆,示例:
class Teacher {
public String name;
}

class Student implements Cloneable {
public String name;
public Teacher teacher;

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

public class CloneTest {

public static void main(String[] args) throws Exception {
Teacher teacher = new Teacher();
teacher.name = "A老师";

Student student = new Student();
student.name = "zero";
student.teacher = teacher;

// 克隆一个Student
Student cloneStu = (Student) student.clone();
System.out.println("cloneStu.name = " + cloneStu.name);
System.out.println("cloneStu.teacher.name = " + cloneStu.teacher.name);
System.out.println(cloneStu.name == student.name);

System.out.println("-----------");
// 修改student的name
student.name = "zero_1";
System.out.println("cloneStu.name = " + cloneStu.name);
// 修改student的teacher.name
student.teacher.name = "B老师";
System.out.println("cloneStu.teacher.name = " + cloneStu.teacher.name);
}
}
运行结果:
cloneStu.name = zero
cloneStu.teacher.name = A老师
true
-----------
cloneStu.name = zero
cloneStu.teacher.name = B老师
      可以看出,clone()方法是浅克隆,因为cloneStu和student的teacher是同一个引用,当student.teacher.name修改时,cloneStu.teacher.name也是变了。那么,问题来了,String也应该是引用, 从cloneStu.name == student.name返回值是true,也可以看出cloneStu.name与student.name是用一个引用,那为什么student.name修改后,cloneStu.name却没有改变呢?

深克隆

示例:
class Teacher implements Cloneable {
public String name;

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

class Student implements Cloneable {
public String name;
public Teacher teacher;

@Override
public Object clone() throws CloneNotSupportedException {
Student cloneStu = null;
cloneStu = (Student) super.clone();
cloneStu.teacher = (Teacher) teacher.clone();
return cloneStu;
}
}

public class CloneTest {

public static void main(String[] args) throws Exception {
Teacher teacher = new Teacher();
teacher.name = "A老师";

Student student = new Student();
student.name = "zero";
student.teacher = teacher;

// 克隆一个Student
Student cloneStu = (Student) student.clone();
System.out.println("cloneStu.name = " + cloneStu.name);
System.out.println("cloneStu.teacher.name = " + cloneStu.teacher.name);
System.out.println(cloneStu.name == student.name);

System.out.println("-----------");
// 修改student的name
student.name = "zero_1";
System.out.println("cloneStu.name = " + cloneStu.name);
// 修改student的teacher.name
student.teacher.name = "B老师";
System.out.println("cloneStu.teacher.name = " + cloneStu.teacher.name);
}
}
运行结果:
cloneStu.name = zero
cloneStu.teacher.name = A老师
true
-----------
cloneStu.name = zero
cloneStu.teacher.name = A老师

利用序列化来做深克隆:

      原理:把对象写到流里的过程是序列化(Serilization)过程,而把对象从流中读出来的过程则叫做反序列化(Deserialization)过程。写在流里的是对象的一个拷贝,而原对象仍然存在于JVM里面。,利用这个特性,可以做深拷贝。

示例:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class Teacher implements Serializable {
private static final long serialVersionUID = 8679913244097902174L;

public String name;
}

class Student implements Serializable {
// serialVersionUID
// 如果对象序列化后存到硬盘上面后,可是后来却更改了类的field(增加或减少或改名),当反序列化时,就会出现Exception的,这样就会造成不兼容性的问题。
// 但当serialVersionUID相同时,它就会将不一样的field以type的缺省值赋值(如int型的是0,String型的是null等),这个可以避开不兼容性的问题。所以最好给serialVersionUID赋值
private static final long serialVersionUID = 5976342672884950801L;

public String name;
public Teacher teacher;

public Object deepCopy() throws Exception {
// 将该对象序列化成流,因为写在流里的是对象的一个拷贝,而原对象仍然存在于JVM里面。所以利用这个特性可以实现对象的深拷贝
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(this);

// 将流序列化成对象
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bis);

return ois.readObject();
}
}

public class CloneTest {

public static void main(String[] args) throws Exception {
Teacher teacher = new Teacher();
teacher.name = "A老师";

Student student = new Student();
student.name = "zero";
student.teacher = teacher;

// 克隆一个Student
Student cloneStu = (Student) student.deepCopy();
System.out.println("cloneStu.name = " + cloneStu.name);
System.out.println("cloneStu.teacher.name = " + cloneStu.teacher.name);
System.out.println(cloneStu.name == student.name);

System.out.println("-----------");
// 修改student的name
student.name = "zero_1";
System.out.println("cloneStu.name = " + cloneStu.name);
// 修改student的teacher.name
student.teacher.name = "B老师";
System.out.println("cloneStu.teacher.name = " + cloneStu.teacher.name);
}
}
运行结果:
cloneStu.name = zero
cloneStu.teacher.name = A老师
false
-----------
cloneStu.name = zero
cloneStu.teacher.name = A老师
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: