java深copy (僞深copy)【原】

 

Teacher.java

package test.clone;

/**
 * 老師
 * 深copy須要實現Cloneable接口
 * @author King
 *
 */
public class Teacher 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
    public String toString() {
        return "Teacher [name=" + name + ", age=" + age + "]";
    }

    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

 

Student.java

package test.clone;

/**
 * 學生
 * 淺copy須要實現Cloneable接口
 * @author King
 *
 */
public class Student implements Cloneable {

    private String name;

    private int age;

    private 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 Teacher getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }

    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + ", Teacher=" + teacher + "]";
    }

    //淺copy
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
    
    /**
     * 淺copy (同clone()方法)
     * 淺copy只對基本類型和String類型作全新複製,
     * 屬性對象引用是不會全新複製的,最終新copy出來的屬性對象引用的仍是同一個堆內存區域,好比teacher屬性
     * @return
     * @throws CloneNotSupportedException
     * ......
     * @time 2018年1月26日 下午8:07:11
     * @author King
     */
    public Object shallowClone() throws CloneNotSupportedException {
        return super.clone();
    }
    
    /**
     * 僞深copy
     * 這種僞深copy模式,其實仍是使用了淺copy技術,只是把屬性對象再次賦了新的淺copy.
     * 當對象比較簡單時能夠用這種模式,由於它比序列化深copy要來得快,仍是定製化copy哪些屬性
     * @return
     * @throws CloneNotSupportedException
     * ......
     * @time 2018年1月26日 下午8:09:39
     * @author King
     */
    public Object deepClone() throws CloneNotSupportedException {
        Student stu = (Student) super.clone();
        Teacher t2 =  (Teacher) teacher.clone();
        stu.setTeacher(t2);
        return stu;
    }

}

 

 

FakeDeepCopy.java 

僞深copy調用樣例java

package test.clone;

/**
 * fakeDeepCopy,實際上是一種僞深copy,對象對比簡單時能夠使用這種技術
* *
@author King * */ public class FakeDeepCopy { public static void main(String[] args) { Teacher techarAAA = new Teacher(); techarAAA.setName("Teacher AAA"); techarAAA.setAge(30); Student studentAAA = new Student(); studentAAA.setName(new String("Student AAA")); studentAAA.setAge(15); studentAAA.setTeacher(techarAAA); System.out.println("學生複製前studentAAA:" + studentAAA); try { Student studentCopy = (Student) studentAAA.clone(); Teacher teacherCopy = studentCopy.getTeacher(); studentCopy.setName(new String("Student BBB")); studentCopy.setAge(20); teacherCopy.setName("Teacher BBB"); teacherCopy.setAge(45); studentCopy.setTeacher(teacherCopy); System.out.println("學生複製後studentAAA:" + studentAAA); System.out.println("學生複製後studentCopy:" + studentCopy); } catch (CloneNotSupportedException e) { e.printStackTrace(); } } }

以上深copy主要經過各層淺copy實現.ide

真正完整深copy可經過序列化的方式.this

相關文章
相關標籤/搜索