public static StudentPrototype CreateInstance() { // MemberwiseClone() 内存复制 // 内存拷贝时 引用属性是拷贝的引用地址 StudentPrototype model = (StudentPrototype)_StudentPrototype.MemberwiseClone(); return(model); }
public static StudentPrototype CreateTance() { StudentPrototype student = (StudentPrototype)_studentPrototype.MemberwiseClone();//克隆一个全新的对象返回出去 //student.Class = new Class()//克隆之后再实列化一下Class《深克隆》 //{ // ClassId = 1, // ClassName = "高级班" //}; return(student); }
public static StudentPrototype CreateInstance() { StudentPrototype studentPrototype = (StudentPrototype)_StudentPrototype.MemberwiseClone();//浅克隆一个对象 studentPrototype.Class = new Class() { Num = 1, Remark = "软谋高级班" }; //这样就是深clone return(studentPrototype); }
//对象浅克隆 public static StudentPrototype CreateInstance() { //MemberwiseClone创建当前object的浅表副本 //浅克隆一个对象。对象里的值类型将被克隆一份新值,对象里的引用类型克隆的新值是相同的引用地址,两个相同的引用地址值是指向同一内存的。 StudentPrototype studentPrototype = (StudentPrototype)_StudentPrototype.MemberwiseClone(); //studentPrototype.Class = new Class() //{ // Num = 1, // Remark = "高级班" //};//可以开辟内存实现深clone return(studentPrototype); }