Polymorphism study notes C # (a) virtual method

  In object-oriented programming, the characteristics of three classes are encapsulation, inheritance, polymorphism. Wherein multi-state specific, dependent on the three methods, i.e. virtual methods , abstract classes and interfaces.

  What is the specific role of polymorphism is it? Or polymorphism exists what is it? Polymorphism exists effectively reduce the coupling procedure, when in use, not only can show we have in common, but also to highlight some special character when necessary.

  So how do you achieve it by three polymorphic method described above?

  Let me talk about today's virtual methods:

  First, we define a human base class. Which contains the person's name and nationality.

class Person
{
    public string Name;
    public string Country;
    public void Introduce()
    { 
      Console.WriteLine (
" My name is " + the Name);
      Console.WriteLine ( "I come from" + Country); }
}

 

  Then we add a class.

class People1:Person
{
    
}

  At this point we were creating an object reference assignment

People1 ch = new People1();
ch.Name = " Bob " ;
ch.Country = " Chinese " ;
ch.Introudce ();


operation result:
My name is Xiao Ming
I come from China

 

  If all are Chinese people, then this method is sufficient competent, but why there is such a person, from the foreign, not Chinese, then this method would introduce a bit of a mouthful. This is a special case, there is a reasonable, we can not put him special because he denied it. At this polymorphism stand out shouted: "My hair volume, let me!."

  Virtual methods: a method in the class declaration before adding the virtual  modifier, it is called a virtual method, whereas non-virtual. And using a virtual  post-modifier, is not allowed then there static, abstract, or override  modifier. With this virtual method, we can very easily solve this special case.

  First, we need to modify the presentation method of the base class, let it empty, and become a virtual method (it means adding a modifier Virtual )

class Person
{
    public  String the Name;
     public  String Country;
     public virtual  void Introudce () // add a modifier virtual presentation so that this method becomes a virtual method
    {
        Console.WriteLine ( " My name is " + the Name);
        Console.WriteLine ( " I come from " + Country);
    }
}

 

   Then we will add this unsocial exception came in, and specifically, a method of making changes to his distinctive, we call rewrite ( the override ) .

class People2
{
    public override void introduce()
    {
        Console.WriteLine("My name is "+Name);
        Console.WriteLine("I'm from "+Country);
    }
}

 

  Next we just need to put into the refrigerator to an elephant. Create an object, assign references

People2 Eup = new People2();
Eup.Name = "Jack";
Eup.Country = "American";
Eup.Introudce();

operation result:
My name is Jack
I'm from American

 

  Does not require large-scale changes, modify it as needed, can be used in the original method does not require modification of the time, this is the advantage of virtual methods.

These are some of my little humble opinion, if there are errors, fortunate enough to be seen seniors, seniors also want to be able to let me know, thanks thanks! ! !

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/wangxiao666/p/11832267.html