C# - 多态性

"多态性"一词意味着具有多种形式。在面向对象编程范式中,多态性通常表示为"一个接口,多个函数"。

多态性可以是静态的,也可以是动态的。在静态多态性中,对函数的响应在编译时确定。在动态多态性中,响应在运行时确定。

静态多态性

在编译时将函数链接到对象的机制称为早期绑定。它也被称为静态绑定。C# 提供了两种实现静态多态性的技术。它们是 −

  • 函数重载
  • 运算符重载

我们将在下一章讨论运算符重载。

函数重载

在同一作用域内,可以对同一个函数名进行多个定义。函数的定义必须在参数列表中的类型和/或参数数量上有所不同。不能对仅返回类型不同的函数声明进行重载。

以下示例显示如何使用函数 print() 打印不同的数据类型 −

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();
      }
   }
}

当编译并执行上述代码时,它会产生以下结果 -

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

动态多态

C# 允许创建抽象类,用于提供接口的部分类实现。当派生类继承该类时,实现即完成。抽象类包含抽象方法,这些方法由派生类实现。派生类具有更专业的功能。

以下是关于抽象类的规则 -

  • 不能创建抽象类的实例

  • 不能在抽象类之外声明抽象方法

  • 当一个类被声明为sealed时,它不能被继承,抽象类不能被声明为sealed。

以下程序演示了一个抽象类 -

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 class area :");
         return (width * length); 
      }
   }
   class RectangleTester {
      static void Main(string[] args) {
         Rectangle r = new Rectangle(10, 7);
         double a = r.area();
         Console.WriteLine("Area: {0}",a);
         Console.ReadKey();
      }
   }
}

当编译并执行上述代码时,它会产生以下结果 -

Rectangle class area :
Area: 70

当你在一个类中定义了一个函数,并希望在继承类中实现它时,可以使用虚函数。这些虚函数在不同的继承类中可以有不同的实现,并且这些函数的调用将在运行时决定。

动态多态性由抽象类虚函数实现。

以下程序演示了这一点 -

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("Parent class area :");
         return 0;
      }
   }
   class Rectangle: Shape {
      public Rectangle( int a = 0, int b = 0): base(a, b) {

      }
      public override int area () {
         Console.WriteLine("Rectangle class area :");
         return (width * height); 
      }
   }
   class Triangle: Shape {
      public Triangle(int a = 0, int b = 0): base(a, b) {
      }
      public override int area() {
         Console.WriteLine("Triangle class area :");
         return (width * height / 2); 
      }
   }
   class Caller {
      public void CallArea(Shape sh) {
         int a;
         a = sh.area();
         Console.WriteLine("Area: {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();
      }
   }
}

当编译并执行上述代码时,它会产生以下结果 -

Rectangle class area:
Area: 70
Triangle class area:
Area: 25