Пример #1
0
        //Calls with cast implicit Knowing When to Use Override and New Keywords
        // "new" keyword is specified, the compiler will issue a warning and the method
        //in the derived class will hide the method in the base class.
        public static void ScenarioNine()
        {
            var derived = new NewStyleTeacher();

            derived.Dance();
            ((Teacher)derived).Dance(); //Call old method-----new
            System.Console.WriteLine("----------");

            var derived2 = new BachataTeacher();

            derived2.Dance();
            ((Teacher)derived2).Dance(); //Call new method-----------override

            // Keep the console open in debug mode.
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
Пример #2
0
        //Classic sample of Polymorphism 2
        // Virtual methods and properties enable derived classes to extend a base class without
        // needing to use the base class implementation of a method
        public static void ScenarioFive()
        {
            BachataTeacher B = new BachataTeacher();

            B.Dance();  // Calls the new method.

            Teacher A = (Teacher)B;

            A.Dance();  // Also calls the new method.

            Teacher D = new BachataTeacher();

            D.Dance(); // Also calls the new method.

            // Keep the console open in debug mode.
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }