-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
Comments
嗯,可以把你的答案贡献出来。 |
/**
* 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;
}
}
} |
@nttd-junchu 对的,可以通过序列化实现深复制 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
参考代码太简单了,比如遇到循环引用对象怎么办?
The text was updated successfully, but these errors were encountered: