Latest web development tutorials

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#允許您使用關鍵字abstract創建抽像類,用於提供接口的部分類的實現。 當一個派生類繼承自該抽像類時,實現即完成。抽像類包含抽象方法,抽象方法可被派生類實現。派生類具有更專業的功能。

請注意,下面是有關抽像類的一些規則:

  • 您不能創建一個抽像類的實例。
  • 您不能在一個抽像類外部聲明一個抽象方法。
  • 通過在類定義前面放置關鍵字sealed ,可以將類聲明為密封類 當一個類被聲明為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 類的面積:");
         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();
      }
   }
}

當上面的代碼被編譯和執行時,它會產生下列結果:

Rectangle 類的面積:
面積: 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("父類的面積:");
         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();
      }
   }
}

當上面的代碼被編譯和執行時,它會產生下列結果:

Rectangle 類的面積:
面積:70
Triangle 類的面積:
面積:25