- 浅复制
package com.mode.demo.clone;
public class CloneTest1 {
public static void main(String[] args) throws CloneNotSupportedException {
Student1 s1 = new Student1("eve0", 18);
Teacher1 teacher = new Teacher1();
teacher.setAge(40);
teacher.setName("ha");
s1.setTeacher(teacher);
Student1 s2 = (Student1) s1.clone();
s2.setAge(23);
s2.setName("二狗子");
Teacher1 t2 = s2.getTeacher();
t2.setAge(60);
t2.setName("zhang");
System.out.println("第二位学生:"+s2.getName() + ":" + s2.getAge());
System.out.println("第二位老师:"+t2.getName() + ":" + t2.getAge());
System.out.println("第一位学生:"+s1.getName() + ":" + s1.getAge());
System.out.println("第一位老师"+teacher.getName() + ":" + teacher.getAge());
}
}
class Student1 implements Cloneable {
private String name;
private int age;
private Teacher1 teacher;
public Student1() {
super();
// TODO Auto-generated constructor stub
}
public Student1(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Teacher1 getTeacher() {
return teacher;
}
public void setTeacher(Teacher1 teacher) {
this.teacher = teacher;
}
@Override
protected Object clone() throws CloneNotSupportedException {
// Student1 s2 = (Student1) super.clone();
// s2.setTeacher((Teacher1) s2.getTeacher().clone());
// return s2;
return super.clone();
}
}
class Teacher1 implements Cloneable {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
protected Object clone() throws CloneNotSupportedException {
// TODO Auto-generated method stub
return super.clone();
}
}
显示结果:
第二位学生:二狗子:23
第二位老师:zhang:60
第一位学生:eve0:18
第一位老师zhang:60
修改s2的值,s1的值也会跟着改变,请看下图
- 深复制
1.实现方式1:修改Student1中的clone方法
@Override
protected Object clone() throws CloneNotSupportedException {
Student1 s2 = (Student1) super.clone();
s2.setTeacher((Teacher1) s2.getTeacher().clone());
return s2;
}
显示结果:
第二位学生:二狗子:23
第二位老师:zhang:60
第一位学生:eve0:18
第一位老师ha:40
2.实现方式2:通过实现Serializable接口方式
public class CloneTest1 {
public static void main(String[] args) throws Exception {
Student1 s1 = new Student1("eve0", 18);
Teacher1 teacher = new Teacher1();
teacher.setAge(40);
teacher.setName("张老师");
s1.setTeacher(teacher);
Student1 s2 = (Student1) s1.deepCopy();
s2.setAge(23);
s2.setName("二狗子");
Teacher1 t2 = s2.getTeacher();
t2.setAge(60);
t2.setName("王老师");
System.out.println("第二位学生:" + s2.getName() + "," + s2.getAge());
System.out.println("第二位老师:" + t2.getName() + "," + t2.getAge());
System.out.println("第一位学生:" + s1.getName() + "," + s1.getAge());
System.out.println("第一位老师:" + teacher.getName() + "," + teacher.getAge());
}
}
class Student1 implements Serializable {
private String name;
private int age;
private Teacher1 teacher;
public Student1() {
super();
// TODO Auto-generated constructor stub
}
public Student1(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Teacher1 getTeacher() {
return teacher;
}
public void setTeacher(Teacher1 teacher) {
this.teacher = teacher;
}
public Object deepCopy() throws Exception {
ByteArrayOutputStream os = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(this);
ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray());
ObjectInputStream ois = new ObjectInputStream(bais);
return ois.readObject();
}
}
class Teacher1 implements Serializable{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
显示结果:
第二位学生:二狗子:23
第二位老师:王老师:60
第一位学生:eve0:18
第一位老师张老师:40