Exemplo n.º 1
0
        static public PrototypeClass getInstance()
        {
            ///浅克隆,值对象克隆新的地址,引用对象克隆引用地址,不会克隆引用对象本身
            PrototypeClass prototypeClass = (PrototypeClass)_prototypeClass.MemberwiseClone();

            return(prototypeClass);
        }
Exemplo n.º 2
0
        static public PrototypeClass getInsDeep()
        {
            ///浅克隆,值对象克隆新的地址,引用对象克隆引用地址,不会克隆引用对象本身
            PrototypeClass prototypeClass = (PrototypeClass)_prototypeClass.MemberwiseClone();

            ///手动深克隆,class是引用,克隆只克隆了地址
            prototypeClass.inside = new ClassInside();
            return(prototypeClass);
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            {
                Console.WriteLine("-------------- 浅克隆 ---------------");
                PrototypeClass prototypeClass1 = PrototypeClass.getInstance();
                PrototypeClass prototypeClass2 = PrototypeClass.getInstance();

                prototypeClass1.ID          = 5;
                prototypeClass1.inside.CID  = 10;
                prototypeClass1.inside.name = "jimmy";
                Console.WriteLine($"{prototypeClass1.ID}, {prototypeClass1.inside.CID}, {prototypeClass1.inside.name}");
                Console.WriteLine($"{prototypeClass2.ID}, {prototypeClass2.inside.CID}, {prototypeClass2.inside.name}");
            }
            {
                Console.WriteLine("-------------- 手动深克隆 ---------------");
                PrototypeClass prototypeClass1 = PrototypeClass.getInsDeep();
                PrototypeClass prototypeClass2 = PrototypeClass.getInsDeep();

                prototypeClass1.ID          = 5;
                prototypeClass1.inside.CID  = 10;
                prototypeClass1.inside.name = "jimmy";
                Console.WriteLine($"{prototypeClass1.ID}, {prototypeClass1.inside.CID}, {prototypeClass1.inside.name}");
                Console.WriteLine($"{prototypeClass2.ID}, {prototypeClass2.inside.CID}, {prototypeClass2.inside.name}");
            }

            {
                Console.WriteLine("-------------- 序列化深克隆 ---------------");
                PrototypeClass prototypeClass1 = PrototypeClass.getInsSerial();
                PrototypeClass prototypeClass2 = PrototypeClass.getInsSerial();

                prototypeClass1.ID          = 5;
                prototypeClass1.inside.CID  = 10;
                prototypeClass1.inside.name = "jimmy";
                Console.WriteLine($"{prototypeClass1.ID}, {prototypeClass1.inside.CID}, {prototypeClass1.inside.name}");
                Console.WriteLine($"{prototypeClass2.ID}, {prototypeClass2.inside.CID}, {prototypeClass2.inside.name}");
            }



            Console.ReadLine();
        }
Exemplo n.º 4
0
 /// <summary>
 /// 单例模式一 : 静态构造函数:由CLR保证,在第一次使用这个类之前,调用而且只调用一次
 /// </summary>
 static PrototypeClass()
 {
     _prototypeClass = new PrototypeClass();
 }