-浅复制(浅克隆):被复制的对象的所有变量都含有与原来对象相同的值,而所有的对其他对象的引用仍然指向原来的对象。换言之,浅复制仅仅复制所考虑的对象。而不复制他所引用的对象。
-深复制(深克隆):被复制的对象的所有变量都含有与原来对象相同的值,除去那些引用其他对象的变量。那些引用其他对象的变量将指向被复制过的新对象,而不再是原有的那些被引用的对象。换而言之,深复制把要复制的对象所引用的对象都复制了一遍。
Java的clone()方法【定义在Object类中】
-clone方法将对象复制了一份并返回给调用者。
一般而言,clone()方法满足:
-(1)对任何的对象x,都有x.clone() != x
克隆对象与原对象不是同一个对象
-(2)对任何的对象x,都有
x.clone().getClass() == x.getClass()
克隆对象与原来对象的类型一样
-(3)如果对象x的equals()方法定义恰当,那么x.clone().equals(x)应该成立。
浅复制:
packageclone;public classCloneTest2 {public static void main(String[] args) throwsThrowable {
Teacher teacher= newTeacher();
teacher.setAge(40);
teacher.setName("Teacher zhang");
Student2 s1= newStudent2();
s1.setAge(20);
s1.setName("zhangsan");
s1.setTeacher(teacher);
Student2 s2=(Student2)s1.clone();
System.out.println(s2.getTeacher().getName());
System.out.println(s2.getTeacher().getAge());
System.out.println("----------------------------------------");
teacher.setName("Teacher li");
System.out.println(s2.getTeacher().getName());
System.out.println(s2.getTeacher().getAge());
}
}class Teacher implementsCloneable
{private intage;privateString name;public intgetAge() {returnage;
}public void setAge(intage) {this.age =age;
}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}
}class Student2 implementsCloneable
{private intage;privateString name;privateTeacher teacher;public intgetAge() {returnage;
}public void setAge(intage) {this.age =age;
}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}publicTeacher getTeacher() {returnteacher;
}public voidsetTeacher(Teacher teacher) {this.teacher =teacher;
}
@Overridepublic Object clone() throwsCloneNotSupportedException {//TODO Auto-generated method stub
return super.clone();
}
}
深复制:
packageclone;public classCloneTest2 {public static void main(String[] args) throwsThrowable {
Teacher teacher= newTeacher();
teacher.setAge(40);
teacher.setName("Teacher zhang");
Student2 s1= newStudent2();
s1.setAge(20);
s1.setName("zhangsan");
s1.setTeacher(teacher);
Student2 s2=(Student2)s1.clone();
System.out.println(s2.getTeacher().getName());
System.out.println(s2.getTeacher().getAge());
System.out.println("----------------------------------------");
teacher.setName("Teacher li");
System.out.println(s2.getTeacher().getName());
System.out.println(s2.getTeacher().getAge());
}
}class Teacher implementsCloneable
{private intage;privateString name;public intgetAge() {returnage;
}public void setAge(intage) {this.age =age;
}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}
@Overrideprotected Object clone() throwsCloneNotSupportedException {//TODO Auto-generated method stub
return super.clone();
}
}class Student2 implementsCloneable
{private intage;privateString name;privateTeacher teacher;public intgetAge() {returnage;
}public void setAge(intage) {this.age =age;
}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}publicTeacher getTeacher() {returnteacher;
}public voidsetTeacher(Teacher teacher) {this.teacher =teacher;
}
@Overridepublic Object clone() throwsCloneNotSupportedException {
Student2 student2= (Student2)super.clone();
student2.setTeacher((Teacher)student2.getTeacher().clone());returnstudent2;
}
}
packageclone;public classCloneTest2 {public static void main(String[] args) throwsThrowable {
Teacher teacher= newTeacher();
teacher.setAge(40);
teacher.setName("Teacher zhang");
Student2 s1= newStudent2();
s1.setAge(20);
s1.setName("zhangsan");
s1.setTeacher(teacher);
Student2 s2=(Student2)s1.clone();
System.out.println(s2.getTeacher().getName());
System.out.println(s2.getTeacher().getAge());
System.out.println("----------------------------------------");
teacher.setName("Teacher li");
System.out.println(s2.getTeacher().getName());
System.out.println(s2.getTeacher().getAge());
}
}class Teacher implementsCloneable
{private intage;privateString name;public intgetAge() {returnage;
}public void setAge(intage) {this.age =age;
}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}
@Overrideprotected Object clone() throwsCloneNotSupportedException {//TODO Auto-generated method stub
return super.clone();
}
}class Student2 implementsCloneable
{private intage;privateString name;privateTeacher teacher;public intgetAge() {returnage;
}public void setAge(intage) {this.age =age;
}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}publicTeacher getTeacher() {returnteacher;
}public voidsetTeacher(Teacher teacher) {this.teacher =teacher;
}
@Overridepublic Object clone() throwsCloneNotSupportedException {
Student2 student2= (Student2)super.clone();
student2.setTeacher((Teacher)student2.getTeacher().clone());returnstudent2;
}
}