The first two blogs introduce object-oriented encapsulation and inheritance. Next we will introduce the third feature of object-oriented: polymorphism.
* Definition
The consent operation acts on different objects and can have different interpretations to produce different execution results. This is polymorphism. Polymorphism is implemented through the virtual function method in the accumulation of complex writing in the derived class.
** Type
1. polymorphism during compilation
Polymorphism during compilation is implemented through overloading. For non-Virtual members, during compilation, the system determines the operation based on the passed parameters, returned types, and other information.
2. Runtime polymorphism
The running polymorphism refers to the operation that can be performed only when the system is running. C # The Running polymorphism is achieved through the reproduction of virtual members.
*** Related concepts
1. Virtual Methods
Declaration: use virtual keywords
Call the virtual method because the criminal will determine the class instance of the called object and call the appropriate overwriting method.
Virtual methods can have implementations.
2. Reload and overwrite comparison
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + ICAgIDxzdHJvbmc + labels/J0tTKudPDoaM8L3A + labels "brush: java;"> public class DrawingBase {public virtual void Darw () // added vir Tual, a virtual method, can be overwritten by the quilt class {Console. WriteLine ("I am a common Drawing Object. ") ;}} Public class Line: DrawingBase {public override void Darw () // override, indicating method override {Console. WriteLine (" I Am a Line. ") ;}} Public class Circle: DrawingBase {public override void Darw () // override, indicating method override {Console. writeLine ("I Am a circle") ;}} static void Main (string [] args) {DrawingBase dObj = new DrawingBase [2]; dObj [0] = new Line (); dObj [1] = new Circle (); foreach (DrawingBase drawObj in dObj); drawObj. draw (); return 0 ;}
The running result is:
I am a line.
I am a circle.