The polymorphism of C #:
My understanding is that the same operation, acting on different objects, has different results, that is, the same method will have different implementations when it acts on different objects as needed.
C # polymorphism includes: interface polymorphism, inheritance polymorphism.
Which inherit polymorphism and also include polymorphism implemented by virtual method and implemented by abstract method
For example: The basic animals have a way of eating, but different animals eat things will not be the same, such as wolves eat meat, sheep eat grass, so "eat" This method will be in the derived class to re-implement the following, run, by pointing to the base class pointer, to invoke the implementation of methods in the derived class.
Next, for example, polymorphism.
1. Interface polymorphism
Put the animal "eat" method into an interface (IAnimal), and then let the specific animal class (Wolf/sheep) inherit this interface, and according to their own needs to implement this interface.
Code implementation:
The code is as follows:
Class Program {
static void Main (string[] args) {
New Wolf (). Eat ();
New Sheep (). Eat ();
}
}
public class Wolf:ianimal {
Multi-State implementation
public void Eat () {
Console.WriteLine ("Wolf eats meat!") ”);
}
}
public class Sheep:ianimal {
Multi-State implementation
public void Eat () {
Console.WriteLine ("Sheep eat grass!") ”);
}
}
Interface
Public interface IAnimal {
void Eat ();
}
In addition to the Declaration,Running GuestArticles are original, reproduced please link to the form of the address of this article
Multi-State learning notes for object-oriented programming mechanisms in C #
This address: http://www.paobuke.com/develop/c-develop/pbk23185.html
Related content C # boxed and unboxing Operations example Analysis C # design Pattern Series Tutorial-Simple Factory mode C # call RAR file and get RAR return value Method C # implements two-time subtraction method
Examples of C # Speech recognition usages on the open closure principle of C # design mode C # Implementing the simplest text encryption Method C # Traverse a DataSet Control instance summary
Multi-State learning notes for object-oriented programming mechanisms in C #