Esempio n. 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();
        }
Esempio n. 2
0
        //Classic sample of Polymorphism 3
        // If you want your derived member to have the same name as a member in a base class, but you do not want it to participate in virtual invocation,
        // you can use the new keyword.The new keyword is put before
        // the return type of a class member that is being replaced.The following code provides an
        public static void ScenarioSix()
        {
            NewStyleTeacher B = new NewStyleTeacher();

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

            Teacher A = (Teacher)B;

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

            // Keep the console open in debug mode.
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
Esempio n. 3
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();
        }
Esempio n. 4
0
        //Classic override with "new" witout override
        public static void ScenarioFive()
        {
            NewStyleTeacher teacher = new NewStyleTeacher();

            teacher.Dance();       // Show Child
            teacher.ShowMeDance(); // Show Father
            Console.WriteLine("------------------");


            Teacher newTeacher = (Teacher)teacher;

            newTeacher.Dance();
            newTeacher.ShowMeDance(); // Show father -public virtual void Dance()


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