zoukankan      html  css  js  c++  java
  • 设计模式 原型模式(深拷贝)

    先写一个被引用的类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Prototype
    {
        class WorkExperience:ICloneable
        {
            private string workDate;
            public string WorkDate
            {
                get { return workDate; }
                set { workDate = value; }
            }
    
            private string company;
            public string Company
            {
                get { return company; }
                set { company = value; }
            }
    
            public Object Clone()
            {
                return (Object)this.MemberwiseClone();
            }
        }
    }
    
    

    在写简历类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Prototype
    {
        class Resume:ICloneable
        {
            private string name;
            private string sex;
            private string age;
            private WorkExperience work;
            public Resume(string name)
            {
                this.name = name;
                work = new WorkExperience();
            }
    
            private Resume(WorkExperience work)
            {
                this.work = (WorkExperience)work.Clone();
            }
    
            public void SetPersonalInfo(string sex,string age)
            {
                this.sex = sex;
                this.age = age;
            }
    
            public void SetWorkExperience(string workDate,string company)
            {
                this.work.WorkDate = workDate;
                this.work.Company = company;
            }
    
            public void Display()
            {
                Console.WriteLine("{0} {1} {2}", name, sex, age);
                Console.WriteLine("工作经历:{0} {1}", work.WorkDate, work.Company);
            }
    
            public Object Clone()
            {
                Resume obj = new Resume(this.work);
                obj.name = this.name;
                obj.sex = this.sex;
                obj.age = this.age;
                return obj;
            }
        }
    }
    
    
    

    写测试类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Prototype
    {
        class Program
        {
            static void Main(string[] args)
            {
                Resume a = new Resume("大鸟");
                a.SetPersonalInfo("男", "29");
                a.SetWorkExperience("1990-2000", "xx公司");
    
                Resume b = (Resume)a.Clone();
                b.SetWorkExperience("1998-2006", "yy企业");
    
                Resume c = (Resume)a.Clone();
                c.SetPersonalInfo("男", "24");
    
                a.Display();
                b.Display();
                c.Display();
    
                Console.Read();
            }
        }
    }
    
    
  • 相关阅读:
    Mac电脑kernel_task占用内存过高
    Mac上的聚焦搜索无法查找到应用
    Mac电脑变卡的原因:
    IE浏览器整页截屏程序
    拓扑排序算法的一个应用
    简单演示mySQL后端数据库关系信息逆向加入到PowerDesigner的物理数据模型和概念数据模型中
    解密存储过程或函数
    C#画图
    .NET设计模式开篇
    非重复随机序列生成算法
  • 原文地址:https://www.cnblogs.com/yufenghou/p/6024516.html
Copyright © 2011-2022 走看看