public static void Main()
        {
            var inputLine = Console.ReadLine();
            var dogs      = new List <Dog>();
            var cats      = new List <Cat>();
            var snakes    = new List <Snake>();

            while (inputLine != "I'm your Huckleberry")
            {
                var inputLineParams = inputLine.Split(' ').ToList();

                var type = inputLineParams[0];

                AddAnimal(inputLine, dogs, cats, snakes, type);

                if (type == "talk")
                {
                    var animalToTalk = inputLineParams[1];
                    foreach (var dog in dogs)
                    {
                        if (dog.Name == animalToTalk)
                        {
                            Dog.Talk();
                        }
                    }
                    foreach (var cat in cats)
                    {
                        if (cat.Name == animalToTalk)
                        {
                            Cat.Talk();
                        }
                    }
                    foreach (var snake in snakes)
                    {
                        if (snake.Name == animalToTalk)
                        {
                            Snake.Talk();
                        }
                    }
                }

                inputLine = Console.ReadLine();
            }
            foreach (var dog in dogs)
            {
                Console.WriteLine($"Dog: {dog.Name}, Age: {dog.Age}, Number Of Legs: {dog.NumberOfLegs}");
            }
            foreach (var cat in cats)
            {
                Console.WriteLine($"Cat: {cat.Name}, Age: {cat.Age}, IQ: {cat.IntelligenceQuotient}");
            }
            foreach (var snake in snakes)
            {
                Console.WriteLine($"Snake: {snake.Name}, Age: {snake.Age}, Cruelty: {snake.CrueltyCoefficient}");
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            // Polymorphism
            Animal animal1 = new Dog();

            animal1.Talk();                                               // Wuw!
            Console.WriteLine("animal1 has {0} legs.", animal1.Legs);     // animal1 has 4 legs.
            Console.WriteLine("animal1 type is {0}.", animal1.GetType()); // animal1 type is PolymorphismApplication.Dog

            // Polymorphism
            Animal animal2 = new Cat();

            animal2.Talk();                                               // Miaum!
            Console.WriteLine("animal2 has {0} legs.", animal2.Legs);     // animal2 has 4 legs.
            Console.WriteLine("animal2 type is {0}.", animal2.GetType()); // animal2 type is PolymorphismApplication.Cat

            // Polymorphism
            Animal animal3 = new Chicken();

            animal3.Talk();                                               // CotCot!
            Console.WriteLine("animal3 has {0} legs.", animal3.Legs);     // animal3 has 2 legs.
            Console.WriteLine("animal3 type is {0}.", animal3.GetType()); // animal3 type is PolymorphismApplication.Chicken

            // a lot of different animals in a list
            List <Animal> animals = new List <Animal>();

            animals.Add(new Dog());
            animals.Add(new Cat());
            animals.Add(new Rooster());
            animals.Add(new Chicken());
            animals.Add(new Dog());
            animals.Add(new Cat());
            animals.Add(new Chicken());
            animals.Add(new Dog());
            animals.Add(new Cat());
            animals.Add(new Chicken());
            animals.Add(new Rooster());
            // virtual method talk is invoked on each of the derived classes, not the base class.
            foreach (Animal animal in animals)
            {
                // note we can handle this with one talk-method!
                animal.Talk();

                if (animal is Chicken)
                {
                    Console.WriteLine("This one is a chicken.");
                    Chicken chicken = animal as Chicken;
                    chicken.Talk();
                }
                //Console.WriteLine("ANIMAL IS: " + animal.GetType());
            }
        }