/// <summary> /// 原型模式-浅拷贝 /// </summary> static void Func2() { Sheep sheep = new Sheep { Name = "DuoLi", Age = 10, Color = new SheepColor { Code = "001", Name = "White" } }; Sheep sheep1 = (Sheep)sheep.Clone(); Sheep sheep2 = (Sheep)sheep.Clone(); MyPrint(sheep); MyPrint(sheep1); MyPrint(sheep2); }
static void Main(string[] args) { var jolly = new Sheep("Jolly", "Mountain Sheep"); var dolly = jolly.Clone(); Console.WriteLine(dolly.Type); Console.WriteLine("=========="); dolly.Name = "Dolly"; dolly.Type = "Clone Sheep"; Console.WriteLine(dolly.Name); Console.WriteLine(dolly.Type); }
static void Main(string[] args) { var original = new Sheep("Jolly", "Mountain Sheep"); Console.WriteLine(original.Name); // Jolly Console.WriteLine(original.Category); // Mountain Sheep var cloned = original.Clone(); cloned.Name = "Dolly"; Console.WriteLine(cloned.Name); // Dolly Console.WriteLine(cloned.Category); // Mountain Sheep Console.WriteLine(original.Name); // Jolly Console.Read(); }
static void Main() { // When an object is required that is similar to existing object or // when the creation would be expensive as compared to cloning. var original = new Sheep("Jeremy", "Not a sheep, yo"); Console.WriteLine($"Name: {original.Name}"); Console.WriteLine($"Category: {original.Category}"); var cloned = original.Clone(); cloned.Name = "Dolly"; Console.WriteLine($"Name: {cloned.Name}"); Console.WriteLine($"Category: {cloned.Category}"); Console.WriteLine($"Name: {original.Name}"); }