Implement deep copy of custom types

Source: Internet
Author: User

I wrote a technical blog for the first time. I am very skilled and have a limited level. I would like to give you some advice on anything wrong.

LargeAll knowCIASSIt is a reference type. If a referenced variable is assigned to another referenced variable, this assignment is performed on a shortest copy. The two referenced variables point to the same object in the memory. We have such a classIs to execute this operation.

Code

Public   Class Testclass

{
Private   Int _ X;
Private   Int _ Y;

Public Testclass ( Int X, Int Y)

{
This . _ X = X;
This . _ Y = Y;

}

Public   Int X

{
Set { This . _ X = Value ;}

}

}
Static   Void Main ( String [] ARGs)

{

Testclass class1 =   New Testclass ( 1 , 1 );

Testclass class2 = Class1;

Class2.x =   2 ;

}

In this case, we modifyClass2OfXValueClass1MediumXThe value will also change.

In some cases, what should we do if we need a copy of an existing class instance? At this time, we need to implementIcloneableInterface.

IcloneableThe interface is defined as follows:

Code

[Comvisible ( True )]
Public   Interface Icloneable

{
// Abstract:
// Creates a new object that is a copy of the current instance.
//
// Returned results:
// A new object that is a copy of this instance.
Object Clone ();

}

ComvisibleThe feature is to set whether to allowComClient Access, which is not detailed here.

If we want our custom classes to provide deep copy capabilities, there are two situations: one is that our custom classes do not contain variables of the reference type; in another case, whenIt is a variable that contains the reference type. The following two cases are described respectively.

 

Now ourTestclassThe class has only two member variables._ X, _ Y is a value-type variable. A value-type variable does not contain a pointer to an instance. It contains all the content of the instance.IcloneableInterfaceThe clone () method copies each value type field in order in the method. The clone method is

Public   Object Clone ()

{
// Because all our fields are assigned values in the constructor, you can build a new instance with the same value.
Return New Testclass (_ x, _ y );

}

. NetAll types are ultimately inherited from the system. Object type, so we can continue to improve the above clone method, using the protected method provided by the system. Object Type

Memberwiseclone: This method creates a new type instance and sets the field to be the same as the field of this object. Now our field is of the value type, so this method can obtain the instance'sIndependent copy.

CloneMethod changed:

Public   Object Clone ()
{
// Copy each field of testclass one by one
Return   Base . Memberwiseclone ();
}

Now testclass is changed:

 

Code Public   Class Testclass: icloneable
{
Private   Int _ X;
Private   Int _ Y;

Public Testclass ( Int X, Int Y)
{
This . _ X = X;
This . _ Y = Y;
}

Public int x
{< br> set { This . _ x = value ;}< br> }

Public   Object Clone ()
{
// Copy each field of testclass one by one
Return   Base . Memberwiseclone ();
}
}

In this way, we can obtain a separate copy of testclass, as shown below:

Code Static   Void Main ( String [] ARGs)
{
Testclass class1 =   New Testclass ( 1 , 1 );
Testclass class2 = (Testclass) class1.clone ();
// Changing class2 does not affect class1.
Class2.x =   2 ;
}

Contains the type of the referenced variable field. The method of deep copy is similar to the preceding method, note that the reference type is saved in the stack. All direct value assignment to the pointer pointing to the managed heap and the memberwiseclone method both copy the pointer address and actually point to the same instance. We need to build a new type of instance with the same value in the clone method and return it.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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.