コード例 #1
0
        //Classic sample of Polymorphism 4 Knowing When to Use Override and New Keywords
        public static void ScenarioEight()
        {
            Teacher baseTeacher = new Teacher();

            baseTeacher.ShowMeDance();
            System.Console.WriteLine("----------");

            NewStyleTeacher newStyleTeacher = new NewStyleTeacher();

            newStyleTeacher.ShowMeDance(); //go to the new
            newStyleTeacher.Dance();       ///go to the new
            System.Console.WriteLine("----------");

            BachataTeacher bachataTeacher = new BachataTeacher();

            bachataTeacher.ShowMeDance(); //go to  base
            System.Console.WriteLine("----------");

            StupidTeacher stupidTeacher = new StupidTeacher();

            stupidTeacher.ShowMeDance(); //go to the old

            // Keep the console open in debug mode.
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
コード例 #2
0
        //Classic sample of Polymorphism 4
        // Accessing Base Class Virtual Members from Derived Classes
        // A derived class that has replaced or overridden a method or property
        // can still access the method or property on the base class using the base keyword.
        public static void ScenarioSeven()
        {
            StupidTeacher B = new StupidTeacher();

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



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