Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5.2.0 浅复制和深复制?怎样实现深复制 #62

Open
qianL93 opened this issue Nov 5, 2019 · 3 comments
Open

5.2.0 浅复制和深复制?怎样实现深复制 #62

qianL93 opened this issue Nov 5, 2019 · 3 comments

Comments

@qianL93
Copy link

qianL93 commented Nov 5, 2019

参考代码太简单了,比如遇到循环引用对象怎么办?

@wangbojing
Copy link
Member

嗯,可以把你的答案贡献出来。

@nttd-junchu
Copy link

  /**
   * This method makes a "deep clone" of any object it is given.
   * 
   * @param object origin target should implement Serializable
   */
  public static Object deepClone(Object object) {
    try {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      ObjectOutputStream oos = new ObjectOutputStream(baos);
      oos.writeObject(object);
      ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
      ObjectInputStream ois = new ObjectInputStream(bais);
      return ois.readObject();
    }
    catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
}

@2199088044
Copy link

@nttd-junchu 对的,可以通过序列化实现深复制

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants