The word Polymorphism was first used in biology to indicate that organisms of the same race share the same characteristics.
In C #, polymorphism is defined as: the same operation acts on instances of different classes, different classes will be interpreted differently, and finally different execution results will be generated.
C # supports two types of polymorphism:
Polymorphism during compilation (static Association)
Polymorphism during compilation is implemented through overloading. Method overloading and operator overloading all implement polymorphism during compilation.
For non-Virtual members, the system determines the operation to be implemented based on the transmitted parameters, returned types, and other information during compilation.
Runtime polymorphism (Dynamic Association)
The running polymorphism is the polymorphism that determines the operation C # running according to the actual situation until the system is running.
Implemented by virtual members.
The polymorphism during compilation provides us with a fast running speed, while the polymorphism during runtime brings us a high degree of flexibility and abstraction.
Virtual Method
When the virtual modifier is added before the method declaration in the class, we call it a virtual method, and vice versa.
Static, abstract, or override modifiers are not allowed after virtual modifiers are used.
For non-Virtual Methods, whether called by the instance of the class or called by the instance of the derived class of the class
The method execution method remains unchanged. The execution method of the virtual method can be changed by the derived class.
The change is implemented through method overloading.
The following example illustrates the difference between a virtual method and a non-virtual method:
Program list
Using System;
Class
{
Public void F () {Console. WriteLine ("A.F ");}
Public virtual void G () {Console. WriteLine ("A. G ");}
}
Class B:
{
New public void F () {Console. WriteLine ("B. F ");}
Public override void G () {Console. WriteLine ("B .G ");}
}
Class Test
{
Static void Main (){
B B = new B ();
A a = B;
A. F ();
B. F ();
A. G ();
B. G ();
}
}
In this example, Class A provides two methods: Non-virtual F and virtual method G. Class B provides a new non-virtual method F,
This overwrites the inherited F. Class B also loads the inherited method G. The output is
A.F
B. F
B .G
B .G
Note that in this example, method a. G () actually calls B .G instead of a.g, because the compile value is A, but the run value is B.
Therefore, B has completed the actual call to the method.
Overload the virtual method in the derived class
Let's review common method overloading.
A normal method overload refers to more than two methods in the class, including hidden inherited methods, with the same name,
As long as the parameter type or number of parameters are different, the compiler knows under which method should be called.
The overload of base-class virtual methods is another special form of function overload.
When this virtual function is redefined in a derived class, the required method name, return value type, number of parameters in the parameter table, type, and order are required.
It is exactly the same as the virtual function in the base class. To declare the overload of Virtual Methods in a derived class, add the override keyword to the Declaration,
There cannot be new, static, or virtual modifiers.
The implementation of polymorphism is illustrated by the example of automobile.
Program list:
Using System;
Class vehicle // defines the vehicle class
{
Public int wheels; // Number of public member wheels
Protected float weight; // the weight of the protected member.
Public Vehicle (int w, float g ){
Wheels = W;
Weight = g;
}
Public Virtual void speak (){
Console. writeline ("the W vehicle is speaking! ");
}
};
Class car: Vehicle // defines the car type
{
Int passengers; // number of private member passengers
Public Car (int w, float g, int P): Base (W, g)
{
Wheels = W;
Weight = g;
Passengers = P;
}
Public override void speak (){
Console. writeline ("the car is speaking: di-di! ");
}
}
Class truck: Vehicle // define the truck class
{
Int passengers; // number of private member passengers
Float load; // Private member Load
Public truck (int w, float g, int P float L): Base (W, g)
{
Wheels = W;
Weight = g;
Passengers = P;
Load = L;
}
Public override void speak (){
Console. writeline ("the truck is speaking: Ba-Ba! ");
}
}
Class Test
{
Public static void main (){
Vehicle V1 = new vehicle ();
Car c1 = new Car (4,2, 5 );
Truck t1 = new Truck (6, 5, 3, 10 );
V1.Speak ();
V1 = c1;
V1.Speak ();
C1.Speak ();
V1 = t1;
V1.Speak ();
T1.Speak ();
}
}
From the above example, we can see that