深拷贝
using System.IO;
using System;
namespace 原型模式
{
class Program
{
static void Main(string[] args)
{
Summary s1 = new Summary("小王");
s1.SetPersonalInfo("女", "30");
s1.SetScoreInfo("语文", "90");
Summary s2 = (Summary)s1.Clone();
s2.SetScoreInfo("数学", "80");
Summary s3 = (Summary)s1.Clone();
s3.SetScoreInfo("英语", "70");
s1.Show();
s2.Show();
s3.Show();
Console.Read();
}
class Prototype : ICloneable
{
private string course;
public string Course
{
get { return course; }
set { course = value; }
}
private string score;
public string Score
{
get { return score; }
set { score = value; }
}
public Object Clone()
{
return (Object)this.MemberwiseClone();
}
}
class Summary : ICloneable
{
private string name;
private string sex;
private string age;
private Prototype p;
public Summary(string name)
{
this.name = name;
p = new Prototype();
}
public Summary(Prototype p)
{
this.p = (Prototype)p.Clone();
}
public void SetPersonalInfo(string sex, string age)
{
this.sex = sex;
this.age = age;
}
public void SetScoreInfo(string course, string score)
{
p.Course = course;
p.Score = score;
}
public void Show()
{
Console.WriteLine("{0}{1}{2}", name, sex, age);
Console.WriteLine("{0}:{1}", p.Course, p.Score);
}
public Object Clone()
{
Summary s = new Summary(this.p);
s.name = this.name;
s.sex = this.sex;
s.age = this.age;
return s;
}
}
}
}