Shallow copy implementation

xiaoxiao2021-03-06  70

// Shallow copy implementation //2004.9.2using system;

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

Class mytype: iCloneable {

Public RefType R;

// Reference Type field

Public String Name;

// value type field public int32 agn;

// value type field public mytype (String name, int32 agn) {

THIS.NAME = Name;

THIS.AGE = AGE;

R = New RefType (Name, AGE);

}

Public Object Clone () {

Return memberwiseclone ();

}

}

// Start the class

Class app {

Static void main () {

Mytype m1 = new mytype ("Zhang San", 18);

Mytype m2 = (mytype) m1.clone ();

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 has not changed

Console.writeline (m2.r.name.tostring () "," m2.r.age.tostring ());

// Reference type field value change}

}

转载请注明原文地址:https://www.9cbs.com/read-120676.html

New Post(0)