C#原型模式之深复制实现

SYSTEM空间有ICONEALBE接口。。。因为其太常用。

  1 /*
  2  * Created by SharpDevelop.
  3  * User: home
  4  * Date: 2013/4/21
  5  * Time: 22:20
  6  * 
  7  * To change this template use Tools | Options | Coding | Edit Standard Headers.
  8  */
  9 using System;
 10 
 11 namespace Resume
 12 {
 13     class Program
 14     {
 15         public static void Main(string[] args)
 16         {
 17             Resume a = new Resume("大鸟");
 18             a.SetPersonalInfo("", "24");
 19             a.SetWorkExperience("1998-2000","国务院外事办");
 20             
 21             Resume b = (Resume)a.Clone();
 22             b.SetPersonalInfo("", "26");
 23             b.SetWorkExperience("2000-2003","香港回归事务所");
 24             
 25             Resume c = (Resume)a.Clone();
 26             c.SetPersonalInfo("", "28");
 27             c.SetWorkExperience("2005-2008","华盛顿亚洲战略中心");
 28             
 29             
 30             a.Display();
 31             b.Display();
 32             c.Display();
 33             
 34             
 35             Console.Write("Press any key to continue . . . ");
 36             Console.ReadKey(true);
 37         }
 38     }
 39     class WorkExperience : ICloneable
 40     {
 41         private string workDate;
 42         public string WorkDate
 43         {
 44             get { return workDate; }
 45             set { workDate = value; }
 46         }
 47         private string company;
 48         public string Company
 49         {
 50             get { return company; }
 51             set { company = value; }
 52         }
 53         public Object Clone()
 54         {
 55             return (object)this.MemberwiseClone();
 56         }
 57     }
 58     class Resume : ICloneable
 59     {
 60         private string name;
 61         private string sex;
 62         private string age;
 63         private WorkExperience work;
 64         
 65         public Resume(string name)
 66         {
 67             this.name = name;
 68             work = new WorkExperience();
 69         }
 70         private Resume(WorkExperience work)
 71         {
 72             this.work = (WorkExperience)work.Clone();
 73         }
 74         
 75         public void SetPersonalInfo(string sex, string age)
 76         {
 77             this.sex = sex;
 78             this.age = age;
 79         }
 80         public void SetWorkExperience(string workDate, string company)
 81         {
 82             work.WorkDate = workDate;
 83             work.Company = company;
 84         }
 85         
 86         public void Display()
 87         {
 88             Console.WriteLine("{0} {1} {2}", name, sex, age);
 89             Console.WriteLine("工作经历: {0} {1}", work.WorkDate, work.Company);
 90         }
 91         public object Clone()
 92         {
 93             Resume obj = new Resume(this.work);
 94             obj.name = this.name;
 95             obj.sex = this.sex;
 96             obj.age = this.age;
 97             return obj;
 98         }
 99     }
100 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值