class Student implements Cloneable{ //一定要写克隆接口,不然public Object clone()不会实现。
String name = "ab";
int age = 2;
Teacher t;
Student(String name , int age){
this.age = age;
this.name = name;
}
Student(String name , int age, Teacher t){
this.age = age;
this.name = name;
this.t = t;
}
public Object clone() { //方法重写了Lang包里的clone()方法。
Student o = null;
try {
o = (Student) super.clone(); //因为lang包下的clone()是有异常抛出的!所以要try catch语句捕获.
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
o.t = this.t.clone(); //为了避免引用的拷贝,所以在teacher类里重载了clone()方法。
//然后在学生类里用成员变量t调用teacher里的clone方法,完成对象的克隆。
return o;
}
}
class Teacher implements Cloneable{
String name;
Teacher(String name){
this.name = name;
}
public Teacher clone(){ //重写teacher里的克隆
Teacher o = null;
try {
o = (Teacher)super.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
return o;
}
}
public class Clone_2{
public static void main(String[] args){
Teacher tt = new Teacher("lisi");
Student s3 = new Student("s3",21,tt); //这次利用构造函数重载,新建立了一个聚合老师类的
//学生类。
Student s4;
s4 = (Student)s3.clone(); //s3与s4克隆,由于深拷贝是把S3里的所有非基本数据类型的成员变量的类里重载了clone方法,
//所以name分配了新空间,得到了拷贝。而不是引用的拷贝.
s4.t.name = "new name"; //修改S4里的成员变量teacher的name;
s4.name = "s4";
s4.age = 44;
System.out.println("s3'name: "+s3.name+"/t"+"s1.age "+s3.age);
System.out.println("s24name: "+s4.name+"/t"+"s2.age "+s4.age);
System.out.println("s3'teacher: "+s3.t.name); //name被更改。
System.out.println("s4'teacher: "+s4.t.name);
System.out.println("s3'teacher: "+s3.t); //通过打印S3,S4的teacher的地址则证实了,teacher t不是同一个引用值。
System.out.println("s4'teacher: "+s4.t);
}
}