Polymorphism in C #

Source: Internet
Author: User
The first two features of encapsulation, inheritance, polymorphism, and object-oriented are relatively easy to understand, but understanding polymorphism, especially in-depth understanding, may be difficult for beginners. I always think that the best way to learn Oo is to combine practice, encapsulate and inherit the application in actual work, which can be seen everywhere, but what about polymorphism? It may not be necessary. It may be used inadvertently and will not correspond to the word "polymorphism. I would like to discuss with you that your personal abilities are limited. please correct me if you have any shortcomings.
I have seen a similar question before: What would you do if the examiner asked you to describe polymorphism in one sentence and refine it as much as possible? Of course, there are many answers. Each person has different understandings and expressions, but I tend to describe this: different objects implemented by inheritance call the same method and show different behaviors, called polymorphism.

1. A simple implementation of polymorphism (refer to self-http://www.cnblogs.com/glacierh ):

code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/

--> Public class animal
{< br> Public virtual void eat ()
{< br> console. writeline ( " animal eat... " );
}< BR >}

Public class CAT: animal
{< br> Public override void eat ()
{< br> console. writeline ( " cat eat... " );
}< BR >}

Public class dog: animal
{< br> Public override void eat ()
{< br> console. writeline ( " dog eat... " );
}< BR >}

class Program
{< br> static void main ( string [] ARGs)
{< br> animal [] animals = New animal [ 3 ];

animals [ 0 ] = New animal ();
animals [ 1 ] = New CAT ();
animals [ 2 ] = New dog ();

for ( int I = 0 ; I 3 ; I + )
{< br> animals [I]. eat ();
}< BR >}

Output result: Animal eat...
Cat eat...
Dog eat...

OK. From the above example, we can see that through inheritanceAnimalDifferent objects in the object array are calledEat ()This is the simplest example of polymorphism.

2. If the new rather than override keyword is used in inheritance,

code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/

--> Public class animal
{< br> Public virtual void eat ()
{< br> console. writeline ( " animal eat... " );
}< BR >}

Public ClassCat: Animal
{
Public Override VoidEat ()
{
Console. writeline ("Cat eat...");
}
}

Public   Class Dog: Animal
{
// Note that the new keyword is used instead of the override keyword.
Public   New   Void Eat ()
{
Console. writeline ( " Dog eat... " );
}
}

ClassProgram
{
Static VoidMain (String[] ARGs)
{
Animal animal= NewAnimal ();
Animal. Eat ();

Animal cat= NewCAT ();
Cat. Eat ();

Animal dog= NewDog ();
Dog. Eat ();
}
}

Output result: Animal eat...
Cat eat...
Animal eat...

The result shows that when the derived class dog Of Eat () Usage New Dog Object Animal After the object is called Animal Class Eat () Method. It can be understood: New Keyword to make the dog In Eat () Method and Animal In Eat () Methods become irrelevant, but their names happen to be the same. We can also see that polymorphism depends on override implementation.

3. Multi-Inheritance
If Class A has a virtual method (), Class B inherits from Class A and override method (), and class C inherits from Class B, can Class C continue to override method () and implement polymorphism? See the following example: Public   Class Animal
{
Public   Virtual   Void Eat ()
{
Console. writeline ( " Animal eat " );
}
}

Public class dog: animal
{< br> Public override void eat ()
{< br> console. writeline ( " dog eat " );
}< BR >}

Public class wolfdog: dog
{< br> Public override void eat ()
{< br> console. writeline ( " wolfdog eat " );
}< BR >}

class Program
{< br> static void main ( string [] ARGs)
{< br> animal = New animal ();
animal. eat ();

Animal dog= NewDog ();
Dog. Eat ();

Animal wolfdog= NewWolfdog ();
Wolfdog. Eat ();

}
}

Output result: Animal eat...
Dog eat...
Wolfdog eat...

OK. Now I get a positive answer. No matter how many layers the inheritance relationship contains, you can rewrite the method that has been overwritten in the parent class in the subclass. That is, if the parent class method is usedOverrideThe subclass inherits the method, and you can also useOverrideThe polymorphism in Multi-inheritance is implemented in this way. To terminate this rewrite, you only need to use the rewrite method.SealedKeyword.

To be continued...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.