// Deep copy implementation //2004.9.2using system;
Using system.collections;
Class RefType {
Public String Name;
Public INT32 AGE;
Public RefType (String Name, INT32 AGE)
{
THIS.NAME = Name;
THIS.AGE = AGE;
}
}
// Type MyType implements the ICLONEABLE interface, you can be copied with Class mytype: iCloneable {
Public RefType R;
// Reference Type Field Public String Name;
// value type field
Public INT32 AGE;
// Value type field private arraylist al = new arraylist ();
Public mytype (String name, int32 agn) {
THIS.NAME = Name;
THIS.AGE = AGE;
R = New RefType (Name, AGE);
THIS.Al.Add (this.name);
THIS.Al.Add (this.age);
THIS.Al.Add (this.r);
}
Public Object Clone ()
{
Mytype c = new mytype (this.name, this.age);
C.AL = new arraylist ();
C.Al.Add (this.name);
C.Al.Add (this.age);
C.Al.Add (this.r);
Return C;
}
Public mytype deepclone () {
Return (mytype) this.clone ();
}
}
// Start the class coplass app {
Static void main () {
Mytype m1 = new mytype ("Zhang San", 18);
// Source object M1
Mytype m2 = (mytype) m1.deepclone ();
// Target object M2 is cloned by M1 (deep copy)
Console.writeline (M2.Name.Tostring () "," M2.AGE.toTRING ());
Console.writeline (m2.r.name.tostring () "," m2.r.age.tostring ());
M1.NAME = "Li Si";
M1.AGE = 19;
m1.r.name = "Li Si";
m1.r.age = 19;
Console.writeline (M2.Name.Tostring () "," M2.AGE.toTRING ());
// value type field value does not change Console.Writeline (m2.r.name.tostring () "," m2.r.age.toString ());
// The reference type field value has not changed}
}