Introduction to object clone

Source: Internet
Author: User

Object clone is a very convenient and useful method. It is more valuable in data processing, especially when processing large data volumes.

Now let's talk about my understanding of object clone.

 

There are two types of clone: Deep clone and shallow clone. Both create a new instance of the same type as the original object.

However, the creation process is different:

Shallow clone ):Create a new instance of the same type as the original object, and copy the non-static fields of the original object (static fields do not belong to the class instance ).

If the field is of the value type, perform a step-by-step copy on the field.If the field is of reference type, the referenced object is copied but not copied.

Deep clone ):Create a new instance of the same type as the original object, and copy the non-static fields of the original object (static fields do not belong to the class instance ).

If the field is of the value type, perform a step-by-step copy on the field.If the field is of the reference type, copy the instance to which the reference points, instead of copying the reference.

 

Note that the above text is orange, and the difference is here. Visible,

Similarities:The clone object is different from the reference of the original object (because it is not an instance), and The equals result is false (provided that there is no override object's equals method ).

Differences:Shallow clone object, level 2/Level 3 /.. the reference of the/n-level referenced object is the same (because it is a copied reference and all points to the same instance), so equals (provided that there is no override object's equals method) the referenced object in the object network is true.

The deep clone object is a recursive copy of the object instance in its object Network (rather than a reference), equals must be false.

Note:No matter whether deep or shallow clone is performed, it performs replication instead of new operations, so the class initialization process is not generated in clone, that is, member initialization, class initialization, and so on, it is only a copy of an existing object, and assigns the heap address of the object obtained by copy to the new reference.

 

Demo: object X, A, B, C, X references a, B, a references C, pseudoCodeThe demo is as follows:

Shallow clone
Y=X. shallowclone ();
X. Equals (y)=False;
X. A. Equals (Y.)=True;
X. B. Equals (Y. B)=True;
X. A. C. Equals (Y. A. c)=True;

 

Deep clone
Y=X. deepclone ();
X. Equals (y)=False;
X. A. Equals (Y.)=False;
X. B. Equals (Y. B)=False;
X. A. C. Equals (Y. A. c)=False;

 

In. net, the default protected memberwiseclone () method of the object is a shallow clone. To achieve deep clone,

You must implement it yourself .. NET provides an interface iclone to support deep clone of objects. However, in the final analysis, we still need to implement the clone () method by ourselves, that is, the implementation of this method is not necessarily deep clone, or even clone. It depends on how you implement it. It is only a constraint and requires you to do so, but if you do not do this, it will not be able to (but if you do not do this, it will only cause you trouble in use ).

For example, let's take a look at several iclone classes implemented by. Net itself: array and arraylist. The former is shallow clone, and the latter is deep clone.

Array. Clone
Public   Object Clone ()
{
Return   Base . Memberwiseclone ();
}

 

 
Public ObjectClone ()
{
Return Base. Memberwiseclone ();
}

 

Arraylist. Clone
Public   Virtual   Object Clone ()
{
Arraylist list =   New Arraylist ( This . _ Size );
List. _ size =   This . _ Size;
List. _ version =   This . _ Version;
Array. Copy ( This . _ Items, 0 , List. _ items, 0 , This . _ Size );
Return List;
}

 

 

Here we provide an implementation of the deep clone method using serialization (the premise is that the object in the object network must be serializable)

Serializable deep clone
Public Object deepclone ()
{
If ( Null   =   This ) Return   Null ;
Using (Memorystream stream =   New Memorystream ())
{
Binaryformatter formatter =   New Binaryformatter ();
Formatter. serialize (stream, This );
Stream. Position =   0 ;
Return Formatter. deserialize (Stream );
}
}

 

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.