C# 多態(tài)性

多態(tài)是同一個行為具有多個不同表現(xiàn)形式或形態(tài)的能力。

多態(tài)性意味著有多重形式。在面向?qū)ο缶幊谭妒街?,多態(tài)性往往表現(xiàn)為"一個接口,多個功能"。

多態(tài)性可以是靜態(tài)的或動態(tài)的。在靜態(tài)多態(tài)性中,函數(shù)的響應(yīng)是在編譯時發(fā)生的。在動態(tài)多態(tài)性中,函數(shù)的響應(yīng)是在運(yùn)行時發(fā)生的。

在 C# 中,每個類型都是多態(tài)的,因?yàn)榘ㄓ脩舳x類型在內(nèi)的所有類型都繼承自 Object。

多態(tài)就是同一個接口,使用不同的示例而執(zhí)行不同操作,如圖所:

圖片.png

現(xiàn)實(shí)中,比如我們按下 F1 鍵這個動作:

  • 如果當(dāng)前在 Flash 界面下彈出的就是 AS 3 的幫助文檔;

  • 如果當(dāng)前在 Word 下彈出的就是 Word 幫助;

  • 在 Windows 下彈出的就是 Windows 幫助和支持。

同一個事件發(fā)生在不同的對象上會產(chǎn)生不同的結(jié)果。

靜態(tài)多態(tài)性

在編譯時,函數(shù)和對象的連接機(jī)制被稱為早期綁定,也被稱為靜態(tài)綁定。C# 提供了兩種技術(shù)來實(shí)現(xiàn)靜態(tài)多態(tài)性。分別為:

  • 函數(shù)重載

  • 運(yùn)算符重載

運(yùn)算符重載將在下一章節(jié)討論,接下來我們將討論函數(shù)重載。

函數(shù)重載

您可以在同一個范圍內(nèi)對相同的函數(shù)名有多個定義。函數(shù)的定義必須彼此不同,可以是參數(shù)列表中的參數(shù)類型不同,也可以是參數(shù)個數(shù)不同。不能重載只有返回類型不同的函數(shù)聲明。

下面的示例演示了幾個相同的函數(shù) Add(),用于對不同個數(shù)參數(shù)進(jìn)行相加處理:

using System;
namespace PolymorphismApplication
{
    public class TestData  
    {  
        public int Add(int a, int b, int c)  
        {  
            return a + b + c;  
        }  
        public int Add(int a, int b)  
        {  
            return a + b;  
        }  
    }  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            TestData dataClass = new TestData();
            int add1 = dataClass.Add(1, 2);  
            int add2 = dataClass.Add(1, 2, 3);

            Console.WriteLine("add1 :" + add1);
            Console.WriteLine("add2 :" + add2);  
        }  
    }  
}

下面的示例演示了幾個相同的函數(shù) print(),用于打印不同的數(shù)據(jù)類型:

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

      void print(double f)
      {
         Console.WriteLine("輸出浮點(diǎn)型: {0}" , f);
      }

      void print(string s)
      {
         Console.WriteLine("輸出字符串: {0}", s);
      }
      static void Main(string[] args)
      {
         Printdata p = new Printdata();
         // 調(diào)用 print 來打印整數(shù)
         p.print(1);
         // 調(diào)用 print 來打印浮點(diǎn)數(shù)
         p.print(1.23);
         // 調(diào)用 print 來打印字符串
         p.print("Hello Nhooo");
         Console.ReadKey();
      }
   }
}

當(dāng)上面的代碼被編譯和執(zhí)行時,它會產(chǎn)生下列結(jié)果:

輸出整型: 1
輸出浮點(diǎn)型: 1.23
輸出字符串: Hello Nhooo

動態(tài)多態(tài)性

C# 允許您使用關(guān)鍵字 abstract 創(chuàng)建抽象類,用于提供接口的部分類的實(shí)現(xiàn)。當(dāng)一個派生類繼承自該抽象類時,實(shí)現(xiàn)即完成。抽象類包含抽象方法,抽象方法可被派生類實(shí)現(xiàn)。派生類具有更專業(yè)的功能。

請注意,下面是有關(guān)抽象類的一些規(guī)則:

  • 您不能創(chuàng)建一個抽象類的示例。

  • 您不能在一個抽象類外部聲明一個抽象方法。

  • 通過在類定義前面放置關(guān)鍵字 sealed,可以將類聲明為密封類。當(dāng)一個類被聲明為 sealed 時,它不能被繼承。抽象類不能被聲明為 sealed。

下面的程序演示了一個抽象類:

using System;
namespace PolymorphismApplication
{
   abstract class Shape
   {
       abstract public 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();
      }
   }
}

當(dāng)上面的代碼被編譯和執(zhí)行時,它會產(chǎn)生下列結(jié)果:

Rectangle 類的面積:
面積: 70

當(dāng)有一個定義在類中的函數(shù)需要在繼承類中實(shí)現(xiàn)時,可以使用虛方法。

虛方法是使用關(guān)鍵字 virtual 聲明的。

虛方法可以在不同的繼承類中有不同的實(shí)現(xiàn)。

對虛方法的調(diào)用是在運(yùn)行時發(fā)生的。

動態(tài)多態(tài)性是通過 抽象類虛方法 實(shí)現(xiàn)的。

以下示例創(chuàng)建了 Shape 基類,并創(chuàng)建派生類 Circle、 Rectangle、Triangle, Shape 類提供一個名為 Draw 的虛擬方法,在每個派生類中重寫該方法以繪制該類的指定形狀。

using System;
using System.Collections.Generic;

public class Shape
{
    public int X { get; private set; }
    public int Y { get; private set; }
    public int Height { get; set; }
    public int Width { get; set; }
   
    // 虛方法
    public virtual void Draw()
    {
        Console.WriteLine("執(zhí)行基類的畫圖任務(wù)");
    }
}

class Circle : Shape
{
    public override void Draw()
    {
        Console.WriteLine("畫一個圓形");
        base.Draw();
    }
}
class Rectangle : Shape
{
    public override void Draw()
    {
        Console.WriteLine("畫一個長方形");
        base.Draw();
    }
}
class Triangle : Shape
{
    public override void Draw()
    {
        Console.WriteLine("畫一個三角形");
        base.Draw();
    }
}

class Program
{
    static void Main(string[] args)
    {
        // 創(chuàng)建一個 List<Shape> 對象,并向該對象添加 Circle、Triangle 和 Rectangle
        var shapes = new List<Shape>
        {
            new Rectangle(),
            new Triangle(),
            new Circle()
        };

        // 使用 foreach 循環(huán)對該列表的派生類進(jìn)行循環(huán)訪問,并對其中的每個 Shape 對象調(diào)用 Draw 方法 
        foreach (var shape in shapes)
        {
            shape.Draw();
        }

        Console.WriteLine("按下任意鍵退出。");
        Console.ReadKey();
    }

}

當(dāng)上面的代碼被編譯和執(zhí)行時,它會產(chǎn)生下列結(jié)果:

畫一個長方形
執(zhí)行基類的畫圖任務(wù)
畫一個三角形
執(zhí)行基類的畫圖任務(wù)
畫一個圓形
執(zhí)行基類的畫圖任務(wù)
按下任意鍵退出。

下面的程序演示通過虛方法 area() 來計(jì)算不同形狀圖像的面積:

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

當(dāng)上面的代碼被編譯和執(zhí)行時,它會產(chǎn)生下列結(jié)果:

Rectangle 類的面積:
面積:70
Triangle 類的面積:
面積:25
丰满人妻一级特黄a大片,午夜无码免费福利一级,欧美亚洲精品在线,国产婷婷成人久久Av免费高清