多態(tài)性意味著有多重形式。在面向?qū)ο缶幊谭妒街?,多態(tài)性往往表現(xiàn)為"一個(gè)接口,多個(gè)功能"。
多態(tài)性可以是靜態(tài)的或動(dòng)態(tài)的。在靜態(tài)多態(tài)性中,函數(shù)的響應(yīng)是在編譯時(shí)發(fā)生的。在動(dòng)態(tài)多態(tài)性中,函數(shù)的響應(yīng)是在運(yùn)行時(shí)發(fā)生的。
靜態(tài)多態(tài)性
在編譯時(shí),函數(shù)和對(duì)象的連接機(jī)制被稱為早期綁定,也被稱為靜態(tài)綁定。C# 提供了兩種技術(shù)來實(shí)現(xiàn)靜態(tài)多態(tài)性。分別為:
函數(shù)重載
運(yùn)算符重載
運(yùn)算符重載將在下一章節(jié)討論,接下來我們將討論函數(shù)重載。
函數(shù)重載
您可以在同一個(gè)范圍內(nèi)對(duì)相同的函數(shù)名有多個(gè)定義。函數(shù)的定義必須彼此不同,可以是參數(shù)列表中的參數(shù)類型不同,也可以是參數(shù)個(gè)數(shù)不同。不能重載只有返回類型不同的函數(shù)聲明。
下面的實(shí)例演示了幾個(gè)相同的函數(shù) print(),用于打印不同的數(shù)據(jù)類型:
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(); // 調(diào)用 print 來打印整數(shù) p.print(5); // 調(diào)用 print 來打印浮點(diǎn)數(shù) p.print(500.263); // 調(diào)用 print 來打印字符串 p.print("Hello C++"); Console.ReadKey(); } } }
當(dāng)上面的代碼被編譯和執(zhí)行時(shí),它會(huì)產(chǎn)生下列結(jié)果:
Printing int: 5 Printing float: 500.263 Printing string: Hello C++
動(dòng)態(tài)多態(tài)性
C# 允許您使用關(guān)鍵字 abstract 創(chuàng)建抽象類,用于提供接口的部分類的實(shí)現(xiàn)。當(dāng)一個(gè)派生類繼承自該抽象類時(shí),實(shí)現(xiàn)即完成。抽象類包含抽象方法,抽象方法可被派生類實(shí)現(xiàn)。派生類具有更專業(yè)的功能。
請(qǐng)注意,下面是有關(guān)抽象類的一些規(guī)則:
您不能創(chuàng)建一個(gè)抽象類的實(shí)例。
您不能在一個(gè)抽象類外部聲明一個(gè)抽象方法。
通過在類定義前面放置關(guān)鍵字 sealed,可以將類聲明為密封類。當(dāng)一個(gè)類被聲明為 sealed 時(shí),它不能被繼承。抽象類不能被聲明為 sealed。
下面的程序演示了一個(gè)抽象類:
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(); } } }
當(dāng)上面的代碼被編譯和執(zhí)行時(shí),它會(huì)產(chǎn)生下列結(jié)果:
Rectangle 類的面積: 面積: 70
當(dāng)有一個(gè)定義在類中的函數(shù)需要在繼承類中實(shí)現(xiàn)時(shí),可以使用虛方法。虛方法是使用關(guān)鍵字 virtual 聲明的。虛方法可以在不同的繼承類中有不同的實(shí)現(xiàn)。對(duì)虛方法的調(diào)用是在運(yùn)行時(shí)發(fā)生的。
動(dòng)態(tài)多態(tài)性是通過 抽象類 和 虛方法 實(shí)現(xiàn)的。
下面的程序演示了這點(diǎn):
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í)行時(shí),它會(huì)產(chǎn)生下列結(jié)果:
Rectangle 類的面積: 面積:70 Triangle 類的面積: 面積:25