The so-called "copying objects" generates a "sibling brother" that is exactly the same as the specified object ".
For object replication, there are two scenarios: shallow replication and deep replication.
1. Shallow copy: when the field value of the object is copied, the object referenced by the field will not be copied.
For example, if an object has a field pointing to a string and we perform a shortest copy on the object, the two objects will reference the same string.
2. Deep replication: copying objects referenced by fields in an object example.
For example, if an object has a field pointing to a string and has a deep copy of the object. I will create a new object and a new string. the new object points to the new string.
3. In. net, the default object replication method is light replication. We can achieve deep replication in two ways.
A. The class implements the icloneable interface.
B. Object serialization technology.
I. Light Replication
For objects that only contain simple fields, the process of copying an object can be simplified to "Creating an object of the same type, and then copying the field values one by one.
For example:Code:
Shortest sample code
ClassClassa
{
Public IntAvalue =100;
}
To copy a classa object, follow these steps:
Public StaticClassa cloneobject (classa OBJ)
{
Classa newobj =NewA ();
Newobj. avalue = obj. avalue;
ReturnNewobj;
}
The following is the sample code (assuming they are in the same class as the cloneobject method ):
Classa obja =NewClassa ();
Classa Other = cloneobject (obja );//Copy A classa object
The above object replication process is very simple. However, objects can be combined with each other. If we consider the combination of objects, the problem of object replication is complicated. The following sample code:
Problems caused by shallow Replication
Public Class Classa
{
Public Int I = 100 ;
Public Classb embedobject; // Classa contains a classb object
Public Classa ()
{
Embedobject =New Classb ();
}
Class Classb
{
Public Int Bvalue = 200 ;
}
Based on the principle of "copying objects through field values", modify the following:
Static Classa cloneobject (classa OBJ)
{
Classa newobj = New Classa ();
Newobj. avalue = obj. avalue; // Field Replication
Newobj. embedobject = obj. embedobject; // Reference Replication
Return OBJ;
}
The following is the sample code for copying a classa object:
Classa obja = New Classa ();
Class Other = cloneobject (obja );// Copy A classa object
}
The problem arises. Because the field embedobject in the class classa is a reference type variable, just copy the field and the result is that the old object and the new object "share" A classb object.
The above object replication method is called shallow copy"
In fact, we want to perform "Deep copy deep copy", which features that the contained objects are also copied together.
Ii. Deep Replication
Shortest replication is the default object replication method of. NET Framework. The object class provides a memberwise Method for shortest copying an object.
To achieve deep replication,. NET Framework provides an icloneable interface, which must be implemented for classes that use the "Deep replication" Method for object replication.
PublicInteface icloneable
{
ObjectClone ();
}
The following is the sample code:
Deep copy sample code
Using System;
Using System. Collections. Generic;
Using System. text;
Namespace Objectclone
{
Class Classa: icloneable
{
Public Int Avalue = 100 ;
Public Classb embedobject; // Classa contains a classb object
Object icloneable. Clone ()
{
Classa obja = New Classa ();
Obja. avalue = This . Avalue;
Obja. embedobject = ( This . EmbedobjectAs Icloneable). Clone () As Classb;
Return Obja;
}
}
Class Classb: icloneable
{
Public Int Bvalue = 200 ;
Object icloneable. Clone ()
{
Classb objb = New Classb ();
Objb. bvalue = This . Bvalue;
Return Objb;
}
}
Class Program
{
Static Void Main ( String [] ARGs)
{
Classa obja = New Classa ();
Obja. embedobject = New Classb ();
// Start cloning
Classa Other = (obja As Icloneable). Clone () As Classa;
Console. writeline (other. embedobject = obja. embedobject ); // False
Console. readkey ();
}
}
}
The above code classes A and B both implement the icloneable interface. The clone method of the classa object internally calls the clone method of the classb object it contains to complete the copying of the classb object.
When implementing deep replication for object programming, pay attention to the following points:
1. Require all objects involved in deep replication to implement the icloneable interface, or provide other public methods to replicate themselves;
2. Pay attention to the problem of loop inclusion, that is, Class A contains Class B, Class B contains class C, and class C contains Class. In this case, do not create deadlocks when copying objects.
In fact, the easiest way to copy an object is to mark the class as "[serializable]" and then copy the object using the serialization method. Make a detailed description in the following diary.