当需要根据实例创建新的对象时,可以通过原型模式,继承ICloneable接口,通过MemberwiseClone()方法对对象进行复制,如果被复制的成员是值类型,会复制一个新的成员。如果是引用类型,则只是复制数据的引用,他们会指向同一个地址。 #region 原型模式 class Resume : ICloneable { private string name; private string sex; private string age; private string timeArea; private string company; public Resume(string name) { this.name = name; } public void SetPersonalInfo(string sex, string age) { this.age = age; this.sex = sex; } public void SetWorkExperience(string timeArea, string company) { this.timeArea = timeArea; this.company = company; } public void Display() { Console.WriteLine("{0} {1} {2}",this.name,this.sex,this.age); Console.WriteLine("工作经历:{0} {1}",this.timeArea,this.company); } public object Clone() { return (object)this.MemberwiseClone(); } } #endregion class Program { static void Main(string[] args) { #region 原型模式 Console.WriteLine("原型模式效果演示:"); Resume resume1 = new Resume("小明"); resume1.SetPersonalInfo("男","25"); resume1.SetWorkExperience("2006-2010","IBM"); Resume resume2 =(Resume) resume1.Clone(); resume2.SetPersonalInfo("男", "27"); resume2.SetWorkExperience("2007-2008","Microsoft"); resume1.Display(); resume2.Display(); #endregion } }