在托管内存的辅助下,Java和Python对象申请和释放机制相似,均存在深浅拷贝的概念。java
在深拷贝的选择上,默承认以实现Cloneable接口的clone()方法,可是须要手动指定须要深拷贝的属性,并实现。this
public class State implements Cloneable(){ Map<Long,Integer> reg = HashMap<>(); int count =1; public State clone(){ State st = new State(); st.reg.putAll(this.reg); // 局限性 st.count = this.count; } }
该方法有较大局限性,只适用于较小的特定对象:spa
通常推荐采用第三方库的序列化和反序列化的方式,实现真正的,通用的深拷贝,例如Apache的SerializationUtils库对象
参考blog
https://stackoverflow.com/questions/4081858/about-java-cloneable接口