Object Clone Shallow copy doesn't change variable

后端 未结 3 1317
生来不讨喜
生来不讨喜 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 11:49

    why do you think clone is a shallow copy? see here: http://docs.oracle.com/javase/6/docs/api/java/lang/Object.html#clone()

    As it says - what clone does is implementation dependent. Also a 'shallow copy' doesn't simply create a new reference to the same object - generally it means that a new instance is created with the same internal members as the object copied-from. But if you change a member variable of the new obj to point to something new, you are not changing member vars of original obj.

提交回复
热议问题