static void Main(string[] args)
        {
			Animal[] animalArray = new Animal[3];	// Create animal array.
			animalArray[0] = new Cat();				// Insert Cat class object.
			animalArray[1] = new Bird();			// Insert Bird class object.
			animalArray[2] = new Dog();				// Insert Dog class object.

			foreach (Animal a in animalArray)		
			{
				ILiveBirth b = a as ILiveBirth;		// if implements ILiveBirth..
				if (b != null)
				{
					Console.WriteLine("Baby is called: {0}", b.BabyCalled());
				}
			}
		}
Beispiel #2
0
        static void Main(string[] args)
        {
            Animal[] animals = new Animal[3] { new Bird("Ammie"), new Fox("Foxie"), new Bird("Twinky") };

            foreach(var an in animals)
            {
                if (an is IFlyer)
                {
                    IFlyer flyer = an as IFlyer;
                    flyer.fly();
                }
                else
                {
                    Console.WriteLine("The " + an.classOfAnimal + " " + an.name + " cannot fly!");
                }
            }

            Console.ReadKey();
        }