About copy and clone

Source: Internet
Author: User

In the software world, these two concepts are no stranger to us, such. net object. for memberwiseclone () and prototype design mode clone, copy is more common, such as copying a piece of text.

Essentially, copy and clone are identical. They all use a mechanism to create a new object. Since the difference is not an essential concept, the difference between them must be reflected in two aspects: one is "how" and the other is the "why" closely related to the external environment ".

In terms of "how:

  • Copy is to construct a new object based on the attributes of an existing object. This object has all the attributes of the existing object, but does not include the external attributes referenced by this object. For example, when we copy a document, we do not need to copy the citation. Obviously, we do not care about the actual content of the citation.
  • Clone also constructs a new object based on the attributes of an existing object. Different from copy, the new object generated after clone not only has all attributes of the existing object, but also includes all attributes of the object referenced by the existing object.

From the aspect of "why:

  • The object generated by copy does not include the attributes of the object referenced by the original object. All newly generated objects lack the ability to express themselves, that is, they can only be in the same context as the referenced object. For example, copy a and reference object B are usually located in the same document.
  • Because the clone object has the ability to express itself, it and the referenced object do not have to be in the same context. For example, we can use clone to exchange data between two different documents.

In software development, too many concepts often make the software more complex, because in actual development, we only need to adopt one of them, for another concept, we only need to add an adjective to it. For example:

  • "Shallow copy" = copy; "deep copy" = clone
  • "Memberwise clone" = copy; "clone" = clone

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.