浅复制(浅克隆):被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用仍然指向原来的对象。换言之,浅复制仅仅复制所考虑的对象,而不复制它所引用的对象。
深复制(深克隆):被复制对象的所有变量都含有与原来的对象相同的值,除去那些引用其他对象的变量。那些引用其他对象的变量将指向被复制过的新对象,而不再是原有的那些被引用的对象。换言之,深复制把要复制的对象所引用的对象都复制了一遍。
Java的clone()方法【定义在Object类中】
–clone方法将对象复制了一份并返回给调用者。一般而言,clone()方法满足:
–①对任何的对象x,都有x.clone() !=x
•克隆对象与原对象不是同一个对象
–②对任何的对象x,都有x.clone().getClass()= =x.getClass()
•克隆对象与原对象的类型一样
–③如果对象x的equals()方法定义恰当,那么x.clone().equals(x)应该成立。
Java中对象的克隆
–①为了获取对象的一份拷贝,我们可以利用Object类的clone()方法。
–②在派生类中覆盖基类的clone()方法,并声明为public【Object类中的clone()方法为protected的】。
–③在派生类的clone()方法中,调用super.clone()。
–④在派生类中实现Cloneable接口。
//浅复制
public class CloneTest1
{
public static void main(String[] args) throws Throwable
{
Student student = new Student();
student.setAge(20);
student.setName("zhangsan");
Student student2 = (Student)student.clone();
System.out.println(student2.getAge());
System.out.println(student2.getName());
System.out.println("----------------------");
student2.setName("lisi");
System.out.println(student.getName());
System.out.println(student2.getName());
}
}
class Student implements Cloneable
{
private int age;
private String name;
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
@Override
public Object clone() throws CloneNotSupportedException
{
Object object = super.clone();
return object;
}
}
深复制
public class CloneTest2
{
public static void main(String[] args) throws Exception
{
Teacher teacher = new Teacher();
teacher.setAge(40);
teacher.setName("Teacher zhang");
Student2 s1 = new Student2();
s1.setAge(20);
s1.setName("zhangsan");
s1.setTeacher(teacher);
Student2 s2 = (Student2)s1.clone();
System.out.println(s2.getName());
System.out.println(s2.getAge());
teacher.setName("Teacher Li");
System.out.println(s2.getTeacher().getName());
System.out.println(s2.getTeacher().getAge());
}
}
class Teacher implements Cloneable
{
private int age;
private String name;
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
@Override
public Object clone() throws CloneNotSupportedException
{
return super.clone();
}
}
class Student2 implements Cloneable
{
private int age;
private String name;
private Teacher teacher;
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public Teacher getTeacher()
{
return teacher;
}
public void setTeacher(Teacher teacher)
{
this.teacher = teacher;
}
@Override
public Object clone() throws CloneNotSupportedException
{
Student2 student2 = (Student2)super.clone();
student2.setTeacher((Teacher)student2.getTeacher().clone());
return student2;
}
}
利用序列化实现深复制
public class CloneTest3
{
public static void main(String[] args) throws Exception
{
Teacher3 t = new Teacher3();
t.setAge(40);
t.setName("Teacher Zhang");
Student3 s1 = new Student3();
s1.setAge(20);
s1.setName("zhangsan");
s1.setTeacher(t);
Student3 s2 = (Student3)s1.deepCopy();
System.out.println(s2.getAge());
System.out.println(s2.getName());
System.out.println("-----------------------");
System.out.println(s2.getTeacher().getAge());
System.out.println(s2.getTeacher().getName());
s2.getTeacher().setAge(50);
s2.getTeacher().setName("Teacher Li");
System.out.println(s1.getTeacher().getAge());
System.out.println(s1.getTeacher().getName());
}
}
class Teacher3 implements Serializable
{
private int age;
private String name;
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
class Student3 implements Serializable
{
private int age;
private String name;
private Teacher3 teacher;
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public Teacher3 getTeacher()
{
return teacher;
}
public void setTeacher(Teacher3 teacher)
{
this.teacher = teacher;
}
public Object deepCopy() throws Exception
{
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();
}
}