欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解c# 多態(tài)

 更新時(shí)間:2020年07月20日 08:46:06   作者:菜鳥教程  
這篇文章主要介紹了c# 多態(tài)的相關(guān)資料,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下

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

多態(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ā)生的。

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

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

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

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

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

靜態(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ù) Add(),用于對(duì)不同個(gè)數(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í)例演示了幾個(gè)相同的函數(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 Runoob");
  Console.ReadKey();
 }
 }
}

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

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

動(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
 {
 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í)行時(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)的。

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

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("畫一個(gè)圓形");
 base.Draw();
 }
}
class Rectangle : Shape
{
 public override void Draw()
 {
 Console.WriteLine("畫一個(gè)長方形");
 base.Draw();
 }
}
class Triangle : Shape
{
 public override void Draw()
 {
 Console.WriteLine("畫一個(gè)三角形");
 base.Draw();
 }
}

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

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

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

}

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

畫一個(gè)長方形
執(zhí)行基類的畫圖任務(wù)
畫一個(gè)三角形
執(zhí)行基類的畫圖任務(wù)
畫一個(gè)圓形
執(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í)行時(shí),它會(huì)產(chǎn)生下列結(jié)果:

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

以上就是詳解c# 多態(tài)的詳細(xì)內(nèi)容,更多關(guān)于c# 多態(tài)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論