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();
         
         // Call print to print integer
         p.print(5);
         
         // Call print to print float
         p.print(500.263);
         
         // Call print to print string
         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

當您在類中定義了一個希望在繼承的類(類)中實現的函式時,您使用virtual函式。虛擬函式可以在不同的繼承類中以不同的方式實現,並且對這些函式的呼叫將在執行時決定。

動態多型由抽象類虛擬函式實現。

以下程式演示了這一點:

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
廣告