Object中自带native clone()方法.
研究了一下用法.
public class DeepCopyTest { public static void main(String[] args) throws CloneNotSupportedException { test1();
System.out.println("-----------------------------");
test2(); } static void test1() throws CloneNotSupportedException {
Teacher t1 = new Teacher();
t1.settName("LaoLi"); Teacher t2 = new Teacher();
t2.settName("LaoWang"); Stu stu = new Stu();
stu.setName("name_1");
stu.setAge(23);
stu.setScore(129);
stu.setTeacher(t1); Stu stuCopy = stu.clone();
System.out.println("1:" + stu.toString());
System.out.println("2:" + stuCopy.toString()); stuCopy.setName("name_2");
stuCopy.setAge(25);
stuCopy.setScore(133);
stuCopy.setTeacher(t2); System.out.println("11:" + stu.toString());
System.out.println("22:" + stuCopy.toString()); } static void test2() throws CloneNotSupportedException {
Teacher t1 = new Teacher();
t1.settName("LaoLi"); Teacher t2 = new Teacher();
t2.settName("LaoWang"); Stu stu = new Stu();
stu.setName("name_1");
stu.setAge(23);
stu.setScore(129);
stu.setTeacher(t1); Stu stuCopy = stu.clone();
System.out.println("1:" + stu.toString());
System.out.println("2:" + stuCopy.toString()); stuCopy.setName("name_2");
stuCopy.setAge(25);
stuCopy.setScore(133);
stuCopy.getTeacher().settName("LaoKong"); System.out.println("11:" + stu.toString());
System.out.println("22:" + stuCopy.toString()); } } class Teacher {
private String tName; public String gettName() {
return tName;
} public void settName(String tName) {
this.tName = tName;
} @Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
} class Stu implements Cloneable {
private String name;
private int age;
private Integer score;
private Teacher teacher; public Teacher getTeacher() {
return teacher;
} public void setTeacher(Teacher teacher) {
this.teacher = teacher;
} 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 Integer getScore() {
return score;
} public void setScore(Integer score) {
this.score = score;
} public Stu clone() throws CloneNotSupportedException {
return (Stu) super.clone();
} @Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
} }
执行结果:
1:Stu[name=name_1,age=23,score=129,teacher=Teacher[tName=LaoLi]]
2:Stu[name=name_1,age=23,score=129,teacher=Teacher[tName=LaoLi]]
11:Stu[name=name_1,age=23,score=129,teacher=Teacher[tName=LaoLi]]
22:Stu[name=name_2,age=25,score=133,teacher=Teacher[tName=LaoWang]]
-----------------------------
1:Stu[name=name_1,age=23,score=129,teacher=Teacher[tName=LaoLi]]
2:Stu[name=name_1,age=23,score=129,teacher=Teacher[tName=LaoLi]]
11:Stu[name=name_1,age=23,score=129,teacher=Teacher[tName=LaoKong]]
22:Stu[name=name_2,age=25,score=133,teacher=Teacher[tName=LaoKong]]
基本验证了这样的结论:
基本类型,不变类型可以做到值拷贝效果.不变类型其实是引用发生了变化.
对象类型,实际上是引用拷贝.
设置一个新对象,stuCopy.getTeacher().setName("LaoKong")
引用其实发生了变化.-->指针A指向空间KK, 指针B也指向空间KK, 指针指向的地址KK中的内容替换掉.
而把对象重新设值, stuCopy.setTeacher(t2)
其实是把对象引用的地址指向新的对象. -->指针A指向空间KK, 指针B也指向空间KK, 指针BB指向新的空间KKK.
----------------------------------------------------
/* 浅复制 */
public Object clone() throws CloneNotSupportedException {
Prototype proto = (Prototype) super.clone();
return proto;
} /* 深复制 */
public Object deepClone() throws IOException, ClassNotFoundException { /* 写入当前对象的二进制流 */
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();
}