Esempio n. 1
0
        /// <summary>
        /// Demonstrates aspects of inheritance and interfaces
        /// </summary>
        public static void Main()
        {
            // Dog stored in Dog variable
            Dog dog = new Dog("Spot", "mutt");

            Console.WriteLine("Dog stored as Dog says " + dog.Speak() + " and shouts " + dog.Shout());
            Console.ReadLine();

            // Dog stored in Animal variable
            Animal animal1 = new Dog("Spot", "mutt");

            Console.WriteLine("Dog stored as Animal says " + animal1.Speak() + " and shouts " + animal1.Shout());
            Console.ReadLine();

            // Spider stored in Spider variable
            Spider spider = new Spider("Charlotte", false);

            Console.WriteLine("Spider stored as Spider says " + spider.Speak() + " and shouts " + spider.Shout());
            Console.ReadLine();

            // Spider stored in Animal variable
            Animal animal2 = new Spider("Charlotte", false);

            Console.WriteLine("Spider stored as Animal says " + animal2.Speak() + " and shouts " + animal2.Shout());
            Console.ReadLine();

            // Phone stored as Phone
            Phone phone = new Phone();

            Console.WriteLine("Phone stored as Phone says " + phone.Speak());
            Console.ReadLine();

            // All of the above, stored in an array of Speakers
            Console.WriteLine("All speak simultaneously, in the same order, after being stored as Speakers:");
            Console.WriteLine(SpeakSimultaneously(new Speaker[] { dog, animal1, spider, animal2, phone }));
            Console.ReadLine();
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            //Example eg = new Example();
            //eg.Test();
            //Console.WriteLine("---------------");
            //ExampleChild egChild = new ExampleChild();
            //egChild.Test();
            //egChild.Test2();



            // return;

            //Object tmpObject = egChild;



            Animal fredTheAnimal = new Animal();

            fredTheAnimal.numberOfLegs = 5;
            fredTheAnimal.Speak();



            Dog myDog = new Dog();

            myDog.numberOfLegs = 3;
            myDog.Speak();
            Console.WriteLine("-------");

            Cat myCat = new Cat();

            myCat.Speak();

            Mouse broonsMouse = new Mouse();

            myDog.Speak();
            myCat.Speak();
            broonsMouse.Speak();

            Animal tmpAnimal = myDog;

            tmpAnimal.Speak();


            Animal[] zoo = { myDog, myCat, broonsMouse };


            Spider spider1 = new Spider();

            spider1.SayName();

            foreach (Animal animal in zoo)
            {
                animal.Speak();
                //if (animal is Cat && (animal as Cat).lovesCheese)
                //    Console.WriteLine("Feed me cheese");

                if (animal is Mouse && (animal as Mouse).lovesCheese)
                {
                    Console.WriteLine("Feed me cheese");
                }
            }
        }