how to clone an object in android?

前端 未结 6 1685
醉梦人生
醉梦人生 2021-01-07 18:43

What would be the best way to copy/clone an object in java/android?

rlBodyDataObj rlbo = bdoTable.get(name);

Right now the code assigns an

6条回答
  •  离开以前
    2021-01-07 19:25

    Sometimes you need to modify some fields before returning from the clone() method.

    Check this : http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Object.html#clone(). I pasted the relevant part here for convenience:

    "By convention, the object returned by this method should be independent of this object (which is being cloned). To achieve this independence, it may be necessary to modify one or more fields of the object returned by super.clone before returning it. Typically, this means copying any mutable objects that comprise the internal "deep structure" of the object being cloned and replacing the references to these objects with references to the copies. If a class contains only primitive fields or references to immutable objects, then it is usually the case that no fields in the object returned by super.clone need to be modified."

提交回复
热议问题