Object Clone Shallow copy doesn't change variable

后端 未结 3 1343
生来不讨喜
生来不讨喜 2021-01-15 11:25

I tried to clone an object and change member functions of new clonned object. If it is shallow copy and according to wiki page, shallow copy and original object point to sam

3条回答
  •  长发绾君心
    2021-01-15 12:06

    When you call object.clone(), new object is created. So as a result you get reference to this object. Object with only primitive type field are cloning perfectly. As a result you get a full independent from your obj1 copy. But if your object has fields with references, you need to do a deep cloning.

提交回复
热议问题