設計模式學習筆記--原型模式

來源:互聯網
上載者:User

標籤:

 1 using System; 2  3 namespace Prototype 4 { 5     /// <summary>  6     /// bzyzhang 7     /// 時間:2016/5/24 19:46:36  8     /// 部落格地址:http://www.cnblogs.com/bzyzhang/ 9     /// WorkExperience說明:本代碼著作權歸bzyzhang所有,使用時必須帶上bzyzhang部落格地址 10     /// </summary> 11     public class WorkExperience:ICloneable12     {13         private string workData;14 15         public string WorkData16         {17             get { return workData; }18             set { workData = value; }19         }20 21         private string company;22 23         public string Company24         {25             get { return company; }26             set { company = value; }27         }28 29         public object Clone()30         {31             return (object)this.MemberwiseClone();32         }33     }34 }
View Code
 1 using System; 2  3 namespace Prototype 4 { 5     /// <summary>  6     /// bzyzhang 7     /// 時間:2016/5/24 19:36:45  8     /// 部落格地址:http://www.cnblogs.com/bzyzhang/ 9     /// Resume說明:本代碼著作權歸bzyzhang所有,使用時必須帶上bzyzhang部落格地址 10     /// </summary> 11     public class Resume:ICloneable12     {13         private string name;14         private string sex;15         private string age;16 17         private WorkExperience workExperience;18 19         public Resume(string name)20         {21             this.name = name;22             workExperience = new WorkExperience();23         }24 25         private Resume(WorkExperience work)26         {27             this.workExperience = (WorkExperience)work.Clone();28         }29 30         public void SetPersonalInfo(string sex,string age)31         {32             this.sex = sex;33             this.age = age;34         }35 36         public void SetWorkExperience(string timeArea,string company)37         {38             workExperience.WorkData = timeArea;39             workExperience.Company = company;40         }41 42         public void Display()43         {44             Console.WriteLine("{0}{1}{2}",name,sex,age);45             Console.WriteLine("工作經曆:{0}{1}", workExperience.WorkData, workExperience.Company);46         }47 48         public object Clone()49         {50             Resume obj = new Resume(this.workExperience);51             obj.name = this.name;52             obj.sex = this.sex;53             obj.age = this.age;54 55             return obj;56         }57     }58 }
View Code
 1 using System; 2 namespace Prototype 3 { 4     class Program 5     { 6         static void Main(string[] args) 7         { 8             Resume a = new Resume("大鳥"); 9             a.SetPersonalInfo("男","29");10             a.SetWorkExperience("1998-2000","xx公司");11 12             Resume b = (Resume)a.Clone();13             b.SetWorkExperience("1998-2006","YY企業");14 15             Resume c = (Resume)a.Clone();16             c.SetPersonalInfo("男","24");17             c.SetWorkExperience("1998-2003","ZZ企業");18 19             a.Display();20             b.Display();21             c.Display();22         }23     }24 }
View Code

 

設計模式學習筆記--原型模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.