示例#1
0
        static void Main(string[] args)
        {
            //0. Without Prototype
            Console.WriteLine("---Without Prototype---");
            Person person1 = new Person("Lucas", new Address("Avenue O", "Brooklyn", 11204));

            Person person2 = person1;

            person2.Name = "Eric";

            Console.WriteLine(person1.ToString());
            Console.WriteLine(person2.ToString());
            //End 0. Without Prototype
            Console.WriteLine();

            //1. With Prototype using ICloneable
            Console.WriteLine("---With Prototype using ICloneable---");
            PersonWithICloneable personWithICloneable1 = new PersonWithICloneable("Lucas", new AddressWithICloneable("Avenue O", "Brooklyn", 11204));

            PersonWithICloneable personWithICloneable2 = (PersonWithICloneable)personWithICloneable1.Clone();

            personWithICloneable2.Name           = "Eric";
            personWithICloneable2.Address.Street = "Sethlow";

            Console.WriteLine(personWithICloneable1.ToString());
            Console.WriteLine(personWithICloneable2.ToString());
            Console.WriteLine();
            //End 1. With Prototype using ICloneable

            //2. With Prototype using Copy Constructor
            Console.WriteLine("---With Prototype using ICloneable---");
            PersonWithCopyConstructor personWithCopyConstructor1 = new PersonWithCopyConstructor("Lucas", new AddressWithCopyConstructor("Avenue O", "Brooklyn", 11204));

            PersonWithCopyConstructor personWithCopyConstructor2 = new PersonWithCopyConstructor(personWithCopyConstructor1);

            personWithCopyConstructor2.Name           = "Eric";
            personWithCopyConstructor2.Address.Street = "Sethlow";

            Console.WriteLine(personWithCopyConstructor1.ToString());
            Console.WriteLine(personWithCopyConstructor2.ToString());
            Console.WriteLine();
            //End 2. With Prototype using Copy Constructor
        }
示例#2
0
 public PersonWithCopyConstructor(PersonWithCopyConstructor personWithCopyConstructor)
 {
     Name    = personWithCopyConstructor.Name;
     Address = new AddressWithCopyConstructor(personWithCopyConstructor.Address);
 }