C # Polymorphism

Source: Internet
Author: User

Polymorphism is one of the three mechanisms of object-oriented programming. Its principle is based on the rule that sub-classes inherited from the parent class can be converted to the parent class. In other words, where the parent class can be used, this class can be used as a subclass. when many sub-classes are derived from the parent class, because each sub-class has different code implementations, when the parent class is used to reference these sub-classes, the same operation can show different operation results, which is called polymorphism.
 

1. Understand what polymorphism is

2. How to define a virtual Method

3. How to reload a virtual Method

4. How to Use polymorphism in programs

Another important concept in Object-Oriented Programming is polymorphism. At runtime, you can call methods in the derived class by pointing to the base class pointer. You can put a group of objects in an array and call their methods. In this case, the effect of polymorphism is shown. These objects do not have to be of the same type. Of course, if they all inherit from a class, you can put these derived classes into an array. If these objects have methods of the same name, you can call the methods of the same name for each object. This section describes how to complete these tasks.

1. Listing 9-1. base class with Virtual Methods: DrawingObject. cs

Using System;
Public class DrawingObject
{
Public virtual void Draw ()
{
Console. WriteLine ("Im just a generic drawing object .");
}
}

Description

Listing 9-1 defines the DrawingObject class. This is a base class that can inherit other objects. This class has a method named Draw. The Draw () method carries a virtual modifier, indicating that the derived class of the base class can overload the method. The Draw () method of the DrawingObject class completes the following tasks: output the statement "Im just a generic drawing object." to the console.

2. Listing 9-2. Derived classes with overload Methods: Line. cs, Circle. cs, and Square. cs

Using System;
Public class Line: DrawingObject
{
Public override void Draw ()
{
Console. WriteLine ("Im a Line .");
}
}

Public class Circle: DrawingObject
{
Public override void Draw ()
{
Console. WriteLine ("Im a Circle .");
}
}

Public class Square: DrawingObject
{
Public override void Draw ()
{
Console. WriteLine ("Im a Square .");
}
}

Description

Listing 9-2 defines three classes. These three classes are derived from the DrawingObject class. Each class has a Draw () method with the same name, and each of these Draw () methods has an overload modifier. The overload modifier allows this method to reload the virtual method of its base class at runtime. The condition for implementing this function is that the class is referenced by pointer variables of the base class type.

3. Listing 9-3. Program for implementing polymorphism: DrawDemo. cs

Using System;
Public class DrawDemo
{
Public static int Main (string [] args)
{
DrawingObject [] dObj = new DrawingObject [4];
DObj [0] = new Line ();
DObj [1] = new Circle ();
DObj [2] = new Square ();
DObj [3] = new DrawingObject ();
Foreach (DrawingObject drawObj in dObj)
{
DrawObj. Draw ();
}
Return 0;
}
}

Description

Listing 9-3 demonstrates the implementation of polymorphism, which uses classes defined in listing 9-1 and listing 9-2. In the Main () method of the DrawDemo class, an array is created. The array element is an object of the DrawingObject class. The array named dObj is composed of four DrawingObject objects.

Next, initialize the dObj array. Because the Line, Circle, and Square classes are derived classes of the DrawingObject class, these classes can be used as the types of dObj array elements. If C # does not have this function, you must create an array for each class. Inheritance can be used as a base class member to save programming workload.

After the array is initialized, execute the foreach loop to find each element in the array. In each loop, each element (object) of the dObj array calls its Draw () method. Polymorphism is reflected in: each object's Draw () method is called at runtime. Although the referenced object type in the dObj array is DrawingObject, this does not affect the virtual method Draw () of the DrawingObject class that is reloaded by the derived class (). In the dObj array, you can call the overloaded Draw () method in the derived class by pointing to the pointer of the DrawingObject base class.

The output result is:

Im a Line.
Im a Circle.
Im a Square.
Im just a generic drawing object.

In the DrawDemo program, the reload Draw () method of each derived class is called. In the last row, the virtual method Draw () of the DrawingObject class is executed (). This is because the fourth element of the array is the DrawingObject class object.

Summary
After learning about polymorphism, You can implement a method to overload the basic class virtual method in the derived class. The relationship between the virtual method and the method of the overloaded derived class reflects the polymorphism of C.

Related Article

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.