コード例 #1
0
        static void Main(string[] args)
        {
            HelmetTiger tiger = new HelmetTiger(42, 50, 7);

            tiger.TigerHelmet = new Helmet(16, 2);

            Console.WriteLine("Is the helmet present? {0}", tiger.TigerHelmet != null);
            Console.WriteLine("HelmetTiger's ToString() method: {0}", tiger.ToString());
        }
コード例 #2
0
        static void Main(string[] args)
        {
            //Console.WriteLine("Main method in Register class ...");
            Tiger[] tigers = new Tiger[]
            {
                new Tiger(42, 50, 7),
                new Tiger(101, 53, 3),
                new HelmetTiger(101010, 60, 6),
                new HelmetTiger(666, 59, 5, new Helmet(20, 2))
            };

            //Iterate through the array (foreach), print each tiger
            //Polymorphism / Polymorphie: Call subclass method via baseclass reference
            //Polymorphie nur mit dynamischer Bindung (= virtuelle Methoden)!
            foreach (Tiger tiger in tigers)
            {
                Console.WriteLine("{0}", tiger);

                //Test if current tiger is a HelmetTiger:
                //Console.WriteLine("Is HelmetTiger: {0}", tiger is HelmetTiger);

                //Try to cast from baseclass to subclass => down-cast
                HelmetTiger helmetTiger = tiger as HelmetTiger;

                if (helmetTiger != null)
                {
                    Console.WriteLine("Is HelmetTiger: {0}", helmetTiger.TigerHelmet);
                }
            }

            //Create a new register from the existing array:
            Register reg = new Register(tigers);

            //Print second tiger from register via indexer:
            Console.WriteLine("Second tiger: {0}", reg[1]);

            Tiger firstSearchTiger  = reg.FindTiger(666);
            Tiger secondSearchTiger = reg.FindTiger(777);

            Console.WriteLine("Tiger with ID 666: {0}", firstSearchTiger);
            Console.WriteLine("Tiger with ID 777: {0}", secondSearchTiger);
        }