你如何制作对象的深拷贝? [英] How do you make a deep copy of an object?
本文介绍了你如何制作对象的深拷贝?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
实现深度对象复制功能有点困难.您采取了哪些步骤来确保原始对象和克隆对象不共享任何引用?
It's a bit difficult to implement a deep object copy function. What steps you take to ensure the original object and the cloned one share no reference?
推荐答案
一个安全的方法是序列化对象,然后反序列化.这可确保所有内容都是全新的参考.
A safe way is to serialize the object, then deserialize. This ensures everything is a brand new reference.
这里有一篇文章介绍如何有效地做到这一点.
Here's an article about how to do this efficiently.
注意事项:类可能会覆盖序列化,从而不创建新实例,例如对于单身人士.如果您的类不可序列化,这当然也不起作用.
Caveats: It's possible for classes to override serialization such that new instances are not created, e.g. for singletons. Also this of course doesn't work if your classes aren't Serializable.
这篇关于你如何制作对象的深拷贝?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文