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


本文摘自PHP中文网,作者黄舟,侵删。

有的场合下,我们需要浅复制便能解决问题,因为我们复制出来的实例,仍然引用原来的初始对象。但是有的时候,这是不够的,因为我们复制出来的实例,还要对引用类型做出局部值的修改调整,并且保证不能影响初始对象!

这便要求深度复制了!

需求是这样的:
首先看一下浅复制为什么不能满足我们的要求:我们要复制简历,并且复制出的版本只与模板简历的求职意向中的公司名称不一致。

我们的第一版代码是这样的:

简历模型1.0版本

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

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

1

2

3

4

5

6

public class EducationInfo

{       

public string schoolName { get; set; }       

public DateTime enrollTime { get; set; }       

public DateTime leaveTime { get; set; }

}

还有嵌套的对象WantedJob1:

1

2

3

4

5

6

public class WantedJob1

{       

public string companyName { get; set; }       

public double eanrings { get; set; }

 

}

我们在客户端使用下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

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”

这是典型的浅复制!

不能满足公司名称要区分的要求,继续修改,变为深度复制:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

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:

1

2

3

4

5

6

7

8

9

10

//WantedJob2 实现接口

 public class WantedJob2:ICloneable

 {       

 public string companyName { get; set; }       

 public double eanrings { get; set; }       

 public object Clone()

     {           

     return this.MemberwiseClone();

     }

 }

客户端调用:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

//此处我们需要对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如何由浅变深的示例代码详解的详细内容!

相关阅读 >>

.NET框架-集合相关所有类的思维导图分享

.NET框架-arraylist的代码详解

.NET框架-应用特性和反射检查数据唯一性的示例代码

.NET框架-详解winform技术中组件被容器引用陷阱

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

.NET框架-xml.serialization的思维导图分享

.NET框架- in ,out, ref , paras使用的代码总结

c#中.NET框架的简介

.NET框架-string是value还是reference type的详解

.NET框架-微软给出的c#编程风格代码实例

更多相关阅读请进入《.NET框架》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...