某一天,当你发现有另外一个你存在的时候是高兴还是感到害怕?这也许违背目前人类的道德准则可是对于可以协作的对象来说如果有必要的时候需要动态的生成一个和自己一样的对象.那么无疑是一个好的发展方向.也许你听说了.是的.那就是在你无需知道具体如何复制的时候.只需要一个主动创建对象然后通过Prototype原型来达到你的目的. 在.NET中一切对象都派生自object类.也就是说从一开始就具备了MemberwiseClone方法.所以问题变的在简单不过了. namespace PrototypeNS { using System; interface IPrototype { IPrototype CloneBody(); }
class MyPrototype : IPrototype { public IPrototype CloneBody() { return (IPrototype)MemberwiseClone(); } }
class Cloned { private IPrototype ip;
public void SetPrototype(IPrototype otherP) { ip = otherP; }
public IPrototype Procedurce() { IPrototype XMan; XMan = ip.CloneBody(); return XMan; } }
public class Client { public static int Main(string[] args) { MyPrototype cloneP; Cloned c=new Cloned(); MyPrototype myP = new MyPrototype(); c.SetPrototype(myP); cloneP=c.Procedurce();
return 0; } } } 
|