Пример #1
0
 private static void MyPrint(Sheep sheep)
 {
     Console.Write(JsonSerializer.Serialize(sheep));
     Console.Write($",SheepHashCode:{sheep.GetHashCode()}");
     Console.Write($",ColorHashCode:{sheep.Color.GetHashCode()}");
     Console.WriteLine();
 }
Пример #2
0
        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);
        }
Пример #3
0
        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();
        }
Пример #4
0
        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}");
        }
Пример #5
0
        /// <summary>
        /// 原型模式-深拷贝
        /// </summary>
        static void Func3()
        {
            Sheep sheep = new Sheep
            {
                Name  = "DuoLi",
                Age   = 10,
                Color = new SheepColor {
                    Code = "001", Name = "White"
                }
            };

            Sheep sheep1 = (Sheep)sheep.DeepClone();
            Sheep sheep2 = (Sheep)sheep.DeepClone();

            MyPrint(sheep);
            MyPrint(sheep1);
            MyPrint(sheep2);
        }
Пример #6
0
        /// <summary>
        /// 普通方式clone
        /// </summary>
        static void Func1()
        {
            Sheep sheep = new Sheep
            {
                Name  = "DuoLi",
                Age   = 10,
                Color = new SheepColor {
                    Code = "001", Name = "White"
                }
            };

            Sheep sheep1 = new Sheep {
                Name = sheep.Name, Age = sheep.Age, Color = sheep.Color
            };
            Sheep sheep2 = new Sheep {
                Name = sheep.Name, Color = sheep.Color, Age = sheep.Age
            };

            MyPrint(sheep);
            MyPrint(sheep1);
            MyPrint(sheep2);
        }