两种方式:
1). 实现Cloneable接口并重写Object类中的clone()方法;
2). 实现Serializable接口,通过对象的序列化和反序列化实现克隆,可以实现真正的深度克隆,代码如下
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class MyUtil { private MyUtil() { throw new AssertionError(); } @SuppressWarnings("unchecked") public static <T extends Serializable> T clone(T obj) throws Exception { ByteArrayOutputStream bout = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bout); oos.writeObject(obj); ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bin); return (T) ois.readObject(); // 说明:调用ByteArrayInputStream或ByteArrayOutputStream对象的close方法没有任何意义 // 这两个基于内存的流只要垃圾回收器清理对象就能够释放资源,这一点不同于对外部资源(如文件流)的释放 } }
class Student implements Cloneable{ private String name; private int age; private StudentClass studentClass; private List<String> list = new ArrayList<>(); public Student() { // TODO Auto-generated constructor stub System.out.println("构造方法被调用"); } 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 StudentClass getStudentClass() { return studentClass; } public void setStudentClass(StudentClass studentClass) { this.studentClass = studentClass; } public void add(String aa){ this.list.add(aa); } @Override public Object clone() throws CloneNotSupportedException { // TODO Auto-generated method stub Student stu = (Student) super.clone(); return stu; } @Override public String toString() { return "Student [name=" + name + ", age=" + age + ",class"+studentClass.getStudentClass()+",list"+list+"]"; } }