The concept of C # polymorphism and its application

Source: Internet
Author: User

Polymorphism is one of the three mechanisms in object-oriented programming, and its principle is based on the rule that subclasses that inherit from the parent class can be converted to their parent class. In other words, you can use a parent class where you can use subclasses of that class. When many subclasses derive from the parent class, because each subclass has a different code implementation, So when using the parent class to refer to these subclasses, the same operation can be performed with different operating results, which is called polymorphism.

1. Understand what is C # polymorphism

2. How to define a virtual method

3. How to overload a virtual method

4. How to apply polymorphism in a program

Another important concept in object-oriented programming is C # polymorphism. At run time, you can invoke the method in the derived class by pointing to a pointer to the base class. You can put a group of objects into an array, and then call their methods, in which case the polymorphism is manifested, and these objects do not have to be the same type of object. Of course, if they all inherit from a class, you can put these derived classes into an array. If each of these objects has a method of the same name, you can call each object's method of the same name. This lesson will show you how to accomplish these things.

1. Listing 9-1. Base class with virtual method: DrawingObject.cs

using System;
public class  DrawingObject
{
    public virtual void Draw()
    {
        Console.WriteLine("I'm just  a generic drawing object.");
    }
}

Description

Listing 9-1 defines the Drawingobject class. This is a base class that can be inherited by other objects. The class has a method named Draw (). The Draw () method has a virtual modifier that indicates that the derived class of the base class can overload the method. The Draw () method of the Drawingobject class accomplishes the following: Output statement "I ' m just a generic drawing object." to the console.

2. Listing 9-2. Derived classes with overloaded methods: Line.cs, Circle.cs, and Square.cs

using  System;
public class Line : DrawingObject
{
     public override void Draw()
    {
         Console.WriteLine("I'm a Line.");
    }
}

public class Circle : DrawingObject
{
     public override void Draw()
    {
         Console.WriteLine("I'm a Circle.");
     }
}

public class Square : DrawingObject
{
    public override void Draw()
    {
         Console.WriteLine("I'm a Square.");
    }
}

Description

Listing 9-2 defines three classes. All three classes derive from the Drawingobject class. Each class has a draw () method with the same name, and each of these draw () methods has an overloaded modifier. The overload modifier enables the method to overload its base class's virtual methods at run time by referencing the class by a pointer variable of the base class type.

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.