Object-oriented -- Polymorphism

Source: Internet
Author: User

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.





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.