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 about Ajax cross-domain access in C # network programming in C # detailed analysis C # Multithreading synchronization events and wait handles WinForm pagination control with source download
Understanding C # code generation process C # packing and unpacking principles of C # How to implement Graphics path transformation C # operations CSV file class instance
Multi-State learning notes for object-oriented programming mechanisms in C #