您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 呼伦贝尔分类信息网,免费分类信息发布

.NET框架-Clone如何由浅变深的示例代码详解

2024/3/13 20:24:35发布26次查看
有的场合下,我们需要浅复制便能解决问题,因为我们复制出来的实例,仍然引用原来的初始对象。但是有的时候,这是不够的,因为我们复制出来的实例,还要对引用类型做出局部值的修改调整,并且保证不能影响初始对象!
这便要求深度复制了!
需求是这样的:
首先看一下浅复制为什么不能满足我们的要求:我们要复制简历,并且复制出的版本只与模板简历的求职意向中的公司名称不一致。
我们的第一版代码是这样的:
简历模型1.0版本
public class resumeinfo1:icloneable { public resumeinfo1(string name, string telephone, educationinfo educationinfo,wantedjob1 job) { this._name = name; this._telephone = telephone; this._educationinfo = educationinfo; this._wantedjob = job; } private string _name; public string name { get { return this._name; } } private string _telephone; public string telephone { get { return _telephone; } } private educationinfo _educationinfo; public educationinfo educationinfo { get { return _educationinfo; } } private wantedjob1 _wantedjob; public wantedjob1 wantedjob { get { return _wantedjob; } } public object clone() { return this.memberwiseclone(); } }
里面嵌套的子类教育背景对象educationinfo
public class educationinfo { public string schoolname { get; set; } public datetime enrolltime { get; set; } public datetime leavetime { get; set; } }
还有嵌套的对象wantedjob1:
public class wantedjob1 { public string companyname { get; set; } public double eanrings { get; set; } }
我们在客户端使用下:
educationinfo educationinfo = new educationinfo(); wantedjob1 wantedjob = new wantedjob1(); resumeinfo1 templateresume = new resumeinfo1("qaz", "18810002000", educationinfo, wantedjob); educationinfo.enrolltime = new datetime(2010, 7, 1); educationinfo.leavetime = new datetime(2015, 1, 1); educationinfo.schoolname = "wsx"; wantedjob1.companyname = "edc"; wantedjob1.eanrings = 1000; //假定各个简历版本,仅仅companyname属性值不同。 var res1 = templateresume.clone(); (res1 as resumeinfo1).wantedjob.companyname = "baidu"; var res2 = templateresume.clone(); (res1 as resumeinfo1).wantedjob.companyname = "ali";
但是我们得到了公司名称都是”ali”
这是典型的浅复制!
不能满足公司名称要区分的要求,继续修改,变为深度复制:
public class resumeinfo2:icloneable { public resumeinfo2(string name, string telephone, educationinfo educationinfo,wantedjob2 job) { this._name = name; this._telephone = telephone; this._educationinfo = educationinfo; this._wantedjob = job; } // private void clonejob(wantedjob2 job) { this._wantedjob = job.clone() as wantedjob2; } private string _name; public string name { get { return this._name; } } private string _telephone; public string telephone { get { return _telephone; } } private educationinfo _educationinfo; public educationinfo educationinfo { get { return _educationinfo; } } private wantedjob2 _wantedjob; public wantedjob2 wantedjob { get { return _wantedjob; } } public object clone() { clonejob(this._wantedjob); return new resumeinfo2(_name,_telephone,_educationinfo,_wantedjob); } }
求职意向对象2.0:
//wantedjob2 实现接口 public class wantedjob2:icloneable { public string companyname { get; set; } public double eanrings { get; set; } public object clone() { return this.memberwiseclone(); } }
客户端调用:
//此处我们需要对wantedjob做深复制处理。 educationinfo educationinfo = new educationinfo(); wantedjob2 wantedjob = new wantedjob2(); resumeinfo2 templateresume = new resumeinfo2("qaz", "18810002000", educationinfo, wantedjob); educationinfo.enrolltime = new datetime(2010, 7, 1); educationinfo.leavetime = new datetime(2015, 1, 1); educationinfo.schoolname = "wsx"; wantedjob.companyname = "edc"; wantedjob.eanrings = 1000; //假定各个简历版本,仅仅companyname属性值不同。 var res1 = templateresume.clone(); (res1 as resumeinfo2).wantedjob.companyname = "baidu"; var res2 = templateresume.clone(); (res2 as resumeinfo2).wantedjob.companyname = "ali";
得到不同的公司名称!深度复制成功!
以上就是.net框架-clone如何由浅变深的示例代码详解的详细内容。
呼伦贝尔分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录