使用SerializationUtils.clone(obj)重写clone()实现深克隆

浅克隆:

package com.osc.demo;

import java.util.List;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

public class Teacher implements Cloneable {
	private String name;
	private int age;
	private List<Student> student;

	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 List<Student> getStudent() {
		return student;
	}

	public void setStudent(List<Student> student) {
		this.student = student;
	}

	@Override
	protected Object clone() throws CloneNotSupportedException {
		return super.clone();
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
	}
}
--
package com.osc.demo;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

public class Student {
	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 ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
	}
}
--
package com.osc.demo;

import java.util.ArrayList;
import java.util.List;

public class Test {
	public static void main(String[] args) {
		Student student = new Student();
		student.setName("张柏芝");
		student.setAge(34);
		List<Student> list = new ArrayList<Student>();
		list.add(student);
		Teacher teacher = new Teacher();
		teacher.setName("陈冠希");
		teacher.setAge(33);
		teacher.setStudent(list);

		try {
			Teacher cloneTeacher = (Teacher) teacher.clone();
			System.out.println("修改前teacher:" + teacher);
			System.out.println("修改前cloneTeacher:" + cloneTeacher);
			System.out.println("--modify--");
			student.setName("钟欣桐");
			student.setAge(33);
			System.out.println("修改后teacher:" + teacher);
			System.out.println("修改后cloneTeacher:" + cloneTeacher);
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
	}
}

--

修改前teacher:Teacher[name=陈冠希,age=33,student=[Student[name=张柏芝,age=34]]]
修改前cloneTeacher:Teacher[name=陈冠希,age=33,student=[Student[name=张柏芝,age=34]]]
--modify--
修改后teacher:Teacher[name=陈冠希,age=33,student=[Student[name=钟欣桐,age=33]]]
修改后cloneTeacher:Teacher[name=陈冠希,age=33,student=[Student[name=钟欣桐,age=33]]]

深克隆:

package com.osc.demo;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.lang.SerializationUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

@SuppressWarnings("serial")
public class Teacher implements Cloneable, Serializable {
	private String name;
	private int age;
	private List<Student> student;

	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 List<Student> getStudent() {
		return student;
	}

	public void setStudent(List<Student> student) {
		this.student = student;
	}

	@Override
	protected Object clone() throws CloneNotSupportedException {
		return SerializationUtils.clone(this);
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
	}
}

--

package com.osc.demo;

import java.io.Serializable;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

@SuppressWarnings("serial")
public class Student 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;
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
	}
}
--
package com.osc.demo;

import java.util.ArrayList;
import java.util.List;

public class Test {
	public static void main(String[] args) {
		Student student = new Student();
		student.setName("张柏芝");
		student.setAge(34);
		List<Student> list = new ArrayList<Student>();
		list.add(student);
		Teacher teacher = new Teacher();
		teacher.setName("陈冠希");
		teacher.setAge(33);
		teacher.setStudent(list);

		try {
			Teacher cloneTeacher = (Teacher) teacher.clone();
			System.out.println("修改前teacher:" + teacher);
			System.out.println("修改前cloneTeacher:" + cloneTeacher);
			System.out.println("--modify--");
			student.setName("钟欣桐");
			student.setAge(33);
			System.out.println("修改后teacher:" + teacher);
			System.out.println("修改后cloneTeacher:" + cloneTeacher);
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
	}
}
--

修改前teacher:Teacher[name=陈冠希,age=33,student=[Student[name=张柏芝,age=34]]]
修改前cloneTeacher:Teacher[name=陈冠希,age=33,student=[Student[name=张柏芝,age=34]]]
--modify--
修改后teacher:Teacher[name=陈冠希,age=33,student=[Student[name=钟欣桐,age=33]]]
修改后cloneTeacher:Teacher[name=陈冠希,age=33,student=[Student[name=张柏芝,age=34]]]

你可能感兴趣的:(使用SerializationUtils.clone(obj)重写clone()实现深克隆)