How do you make a deep copy of an object?

前端 未结 19 1857
执念已碎
执念已碎 2020-11-21 23:09

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?

19条回答
  •  别跟我提以往
    2020-11-21 23:58

    import com.thoughtworks.xstream.XStream;
    
    public class deepCopy {
        private static  XStream xstream = new XStream();
    
        //serialize with Xstream them deserialize ...
        public static Object deepCopy(Object obj){
            return xstream.fromXML(xstream.toXML(obj));
        }
    }
    

提交回复
热议问题