详解java中的深拷贝和浅拷贝(clone()方法的重写、使用序列化实现真正的深拷贝)

  public class TestString {

  public static void main(String[] args) {

  TestString test = new TestString();

  System.out.println("-------浅拷贝---------");

  test.qianCopyTest();

  System.out.println();

  System.out.println("--------使用clone深拷贝--------");

  test.defaultCloneTest();

  System.out.println();

  System.out.println("--------使用序列化实现对象的拷贝--------");

  test.streamClonrTest();

  System.out.println("--------耗时对比--------");

  System.out.println("耗时1 : "+ test.qianCopyCost());

  System.out.println("耗时2 : "+ test.CloneCopyCost());

  System.out.println("耗时3 : "+ test.StreamCopyCost());

  }

  /*浅拷贝*/

  private void qianCopyTest() {

  String s = "cd";

  change(s);

  System.out.println(s);

  System.out.println("----------------");

  String b = new String("cd");

  change(b);

  System.out.println(b);

  System.out.println("----------------");

  int me = 1;

  change(me);

  System.out.println(me);

  System.out.println("----------------");

  Person person = new Person("我", 13,new Email("我"));

  change(person);

  System.out.println(person.toString());

  }

  /*使用默认的clone方法,需要Person实现Cloneable接口*/

  private void defaultCloneTest(){

  Person person = new Person("我", 13,new Email("我"));

  Person person1 = person.clone();

  Person person2 = person.clone();

  System.out.println("person : 【"+person+"】");

  System.out.println("person1 : 【"+person1+"】");

  System.out.println("person2 : 【"+person2+"】");

  //改一个就会触动全部!! 这就是使用默认的clone方法的弊端

  /*该clone()方法是使用Object类的clone()方法,但是该方法存在一个缺陷,它并不会将对象的所有属性全部拷贝过来,而是有选择性的拷贝,基本规则如下:

  1、 基本类型

  如果变量是基本很类型,则拷贝其值,比如int、float等。

  2、 对象

  如果变量是一个实例对象,则拷贝其地址引用,也就是说此时新对象与原来对象是公用该实例变量。

  3、 String字符串

  若变量为String字符串,则拷贝其地址引用。但是在修改时,它会从字符串池中重新生成一个新的字符串,原有紫都城对象保持不变。*/

  person.getEmail().setContent("你");

  System.out.println("之后的person : 【"+person+"】");

  System.out.println("之后的person1 : 【"+person1+"】");

  System.out.println("之后的person2 : 【"+person2+"】");

  }

  /*使用序列化实现对象的拷贝,需要对象以及对象中的其他对象都要实现Serializable接口*/

  private void streamClonrTest(){

  Person person = new Person("我", 13,new Email("我"));

  Person person1 = CloneUtils.clone(person);

  Person person2 = CloneUtils.clone(person);

  System.out.println("person : 【"+person+"】");

  System.out.println("person1 : 【"+person1+"】");

  System.out.println("person2 : 【"+person2+"】");

  person.getEmail().setContent("你");

  System.out.println("之后的person : 【"+person+"】");

  System.out.println("之后的person1 : 【"+person1+"】");

  System.out.println("之后的person2 : 【"+person2+"】");

  }

  private static void change(String x) {

  x = "ab";

  }

  private static void change(int x) {

  x = 2;

  }

  private static void change(Person x) {

  x = new Person("你", 20, new Email("你"));

  }

  private long qianCopyCost(){

  long start = System.currentTimeMillis();

  Person person = new Person("我", 13,new Email("我"));

  List list = new ArrayList<>();

  for(int i = 0;i<=10000;i++){

  list.add(new Person("你", 20, new Email("你")));

  }

  return System.currentTimeMillis()-start;

  }

  private long CloneCopyCost(){

  long start = System.currentTimeMillis();

  Person person = new Person("我", 13,new Email("我"));

  List list = new ArrayList<>();

  for(int i = 0;i<=10000;i++){

  list.add(person.clone());

  }

  return System.currentTimeMillis()-start;

  }

  private long StreamCopyCost(){

  long start = System.currentTimeMillis();

  Person person = new Person("我", 13,new Email("我"));

  List list = new ArrayList<>();

  for(int i = 0;i<=10000;i++){

  list.add(CloneUtils.clone(person));

  }

  return System.currentTimeMillis()-start;

  }

  }

  class Person implements Serializable, Cloneable {

  private static final long serialVersionUID = -8584225043397465132L;

  private String name;

  private int age;

  public void setEmail(Email email) {

  this.email = email;

  }

  private Email email;

  public Email getEmail() {

  return email;

  }

  public void setName(String name) {

  this.name = name;

  }

  public void setAge(int age) {

  this.age = age;

  }

  public Person(String name, int age, Email email) {

  this.name = name;

  this.age = age;

  this.email = email;

  }

  @Override

  public String toString() {

  return "name : " + name + " | age : " + age +" | content : "+email.getContent();

  }

  @Override

  protected Person clone() {

  Person person = null;

  try {

  person = (Person) super.clone();

  /*如果加上下一行 “使用clone深拷贝” 就不会改一处其他都改变了*/

  person.setEmail(new Email(person.getEmail().getContent()));

  } catch (CloneNotSupportedException e) {

  e.printStackTrace();

  }

  return person;

  }

  }

  class Email implements Serializable {

  private static final long serialVersionUID = 1426052929769365539L;

  private String content;

  public void setContent(String content) {

  this.content = content;

  }

  public String getContent() {

  return content;

  }

  public Email(String content) {

  this.content = content;

  }

  }