Virtual Method and Polymorphism

Time:2019-6-8
class Program
     {
         static void Main(string[] args)
         {
             //Dog dog = new Dog();
             Pet dog = new Dog ("DD"); // Create an instance of the base class Pet pointing to the derived class Dog
             //dog.Name = "DD";
             Dog. Speak (); // Here is the call to the virtual method Speak () of the Pet class, and the call to the same name rewrite method Speak () - - that is, polymorphism, according to the derived class Dog to which Dog refers.
             //Cat cat = new Cat();
             Pet cat = new Cat ("CC"); create an instance of the base class Pet pointing to the derived Cat class
             //cat.Name = "CC";
             Cat. Speak (); // Here is the call to the virtual method Speak () of the Pet class, and the call to the same name rewrite method Speak () - - polymorphism, according to the derived class Cat that cat refers to.
           
 
         }
 
     }
     public class Pet
     {
         public string Name;
         public void PrintName()
         {
             Console.WriteLine("Print"+Name);
         }
         
         Virtual public void Speak ()// declares that virtual methods are virtual methods that can be overrided in derived classes
         {
             Console.WriteLine("I'm "+Name);  
         }
     }
     public class Dog : Pet
     {
         Public Dog (string name) // constructor
         {
             Name = name;
         }
         new public void PrintName()
         {
             Console. WriteLine ("sub-printing"+Name);
         }
         Override public void Speak ()// Rewrite the virtual method of base class Pet
         {
             Console. WriteLine ("sub-theory"+Name);
         }
     }
     public class Cat : Pet
     {
         Public Cat (string name) // constructor
         {
             Name = name;
         }
         new public void PrintName()
         {
             Console.WriteLine ("sub-printing"+Name);
         }
         Override public void Speak ()// Rewrite the virtual method of base class Pet
         {
             Console. WriteLine ("sub-theory"+Name);
         }
     }

These are virtual methods and polymorphism.

In the process of object-oriented programming, we should try to rely on abstract classes rather than specific classes.

References to base classes can point to derived classes. By referencing the Pet class pointing to the Dog class, only the portion inherited from Pet in the Dog can be accessed, as follows:

class Program
    {
        static void Main(string[] args)
        {
            
            Pet dog = new Dog ("DD"); // Create an instance of the base class Pet pointing to the derived class Dog, and the location of the previous Pet determines which class the method referred to below is taken from.
            dog.PrintName();
            Pet cat = new Cat ("CC"); create an instance of the base class Pet pointing to the derived Cat class
                         
           Cat. PrintName (); /// Reference to Pet in the derived Cat class accesses the method inherited from Pet in Catl - - Reference to the base class

        }

    }
    public class Pet
    {
        public string Name;
        public void PrintName()
        {
            Console.WriteLine("Print"+Name);
        }
             
    }
    public class Dog : Pet
    {
        Public Dog (string name) // constructor
        {
            Name = name;
        }
        new public void PrintName()
        {
            Console. WriteLine ("sub-printing"+Name);
        }
      
    }
    public class Cat : Pet
    {
        Public Cat (string name) // constructor
        {
            Name = name;
        }
        new public void PrintName()
        {
            Console.WriteLine ("sub-printing"+Name);
        }
       
    }