Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The polymorphism of C


May 11, 2021 C#


Table of contents


C# Polymorphism

Polymorphism means that there are multiple forms. In the object-oriented programming paradigm, polymorphism is often expressed as "one interface, multiple functions".

Polymorphism can be static or dynamic. I n static polymorphism, the response of a function occurs at compile time. In dynamic polymorphism, the response of a function occurs at runtime.

Static polymorphism

At compile time, the connection mechanism between functions and objects is called early binding, also known as static binding. T wo techniques are available to achieve static polymorphism. They are:

  • The function is overloaded
  • The operator is overloaded

Operator overloading is discussed in the next section, and we'll discuss function overloading next.

The function is overloaded

You can have multiple definitions of the same function name in the same scope. F unctions must be defined differently from each other, between different types of arguments in the argument list or different numbers of parameters. You cannot overload only function declarations that return different types.

The following example demonstrates several of the same function print() for printing different data types:

using System;
namespace PolymorphismApplication
{
   class Printdata
   {
      void print(int i)
      {
         Console.WriteLine("Printing int: {0}", i );
      }

      void print(double f)
      {
         Console.WriteLine("Printing float: {0}" , f);
      }

      void print(string s)
      {
         Console.WriteLine("Printing string: {0}", s);
      }
      static void Main(string[] args)
      {
         Printdata p = new Printdata();
         // 调用 print 来打印整数
         p.print(5);
         // 调用 print 来打印浮点数
         p.print(500.263);
         // 调用 print 来打印字符串
         p.print("Hello C++");
         Console.ReadKey();
      }
   }
}

When the above code is compiled and executed, it produces the following results:

Printing int: 5
Printing float: 500.263
Printing string: Hello C++

Dynamic polymorphism

It allows you to create abstract classes using the keyword abstract to provide implementations of some of the classes of the interface. W hen a derived class is inherited from the abstract class, the implementation is complete. sses contain abstract methods, which can be derived from class implementations. Derived classes have more specialized functionality.

Note that here are some rules about abstract classes:

  • You cannot create an instance of an abstract class.
  • You cannot declare an abstract method outside an abstract class.
  • You can declare a class as a sealed class by placing the keyword sealed before the class definition. W hen a class is declared sealed, it cannot be inherited. Abstract classes cannot be declared sealed.

The following program demonstrates an abstract class:

using System;
namespace PolymorphismApplication
{
   abstract class Shape
   {
      public abstract int area();
   }
   class Rectangle:  Shape
   {
      private int length;
      private int width;
      public Rectangle( int a=0, int b=0)
      {
         length = a;
         width = b;
      }
      public override int area ()
      { 
         Console.WriteLine("Rectangle 类的面积:");
         return (width * length); 
      }
   }

   class RectangleTester
   {
      static void Main(string[] args)
      {
         Rectangle r = new Rectangle(10, 7);
         double a = r.area();
         Console.WriteLine("面积: {0}",a);
         Console.ReadKey();
      }
   }
}

When the above code is compiled and executed, it produces the following results:

Rectangle 类的面积:
面积: 70

You can use the virtual method when there is a function defined in the class that needs to be implemented in the inheritance class. T he virtual method is declared using the keyword virtual. V irtual methods can have different implementations in different inheritance classes. Calls to virtual methods occur at runtime.

Dynamic polymorphism is achieved through abstract classes and virtual methods.

The following program demonstrates this:

using System;
namespace PolymorphismApplication
{
   class Shape 
   {
      protected int width, height;
      public Shape( int a=0, int b=0)
      {
         width = a;
         height = b;
      }
      public virtual int area()
      {
         Console.WriteLine("父类的面积:");
         return 0;
      }
   }
   class Rectangle: Shape
   {
      public Rectangle( int a=0, int b=0): base(a, b)
      {

      }
      public override int area ()
      {
         Console.WriteLine("Rectangle 类的面积:");
         return (width * height); 
      }
   }
   class Triangle: Shape
   {
      public Triangle(int a = 0, int b = 0): base(a, b)
      {
      
      }
      public override int area()
      {
         Console.WriteLine("Triangle 类的面积:");
         return (width * height / 2); 
      }
   }
   class Caller
   {
      public void CallArea(Shape sh)
      {
         int a;
         a = sh.area();
         Console.WriteLine("面积: {0}", a);
      }
   }  
   class Tester
   {
      
      static void Main(string[] args)
      {
         Caller c = new Caller();
         Rectangle r = new Rectangle(10, 7);
         Triangle t = new Triangle(10, 5);
         c.CallArea(r);
         c.CallArea(t);
         Console.ReadKey();
      }
   }
}

When the above code is compiled and executed, it produces the following results:

Rectangle 类的面积:
面积:70
Triangle 类的面积:
面积:25

The difference between an abstract method and a virtual method

  • Virtual methods must have implementation parts, abstract methods do not provide implementation parts, abstract methods are a method of forcing derived class overlay, otherwise derived classes will not be instantiated.
  • Abstract methods can only be declared in abstract classes, not virtual methods. If the class contains abstract methods, the class is also abstract, and it must also be declared abstract.
  • Abstract methods must be overriding in derived classes, similar to interfaces, and virtual methods no longer need to be overriding in derived classes.

Simply put, abstract methods need to be implemented by sub-classes. The virtual method has been implemented and can be overwritten by sub-classes or not, depending on the requirements.

Both abstract and virtual methods can be rewritten by derived classes.