课程表

C# 基础教程

C# 高级教程

C# 工具/手册

工具箱
速查手册

C# 多态性

当前位置:免费教程 » 程序设计 » C#

多态性意味着有多重形式。在面向对象编程范式中,多态性往往表现为"一个接口,多个功能"。

多态性可以是静态的或动态的。在静态多态性中,函数的响应是在编译时发生的。在动态多态性中,函数的响应是在运行时发生的。

静态多态性

在编译时,函数和对象的连接机制被称为早期绑定,也被称为静态绑定。C# 提供了两种技术来实现静态多态性。分别为:

  • 函数重载
  • 运算符重载

运算符重载将在下一章节讨论,接下来我们将讨论函数重载。

函数重载

您可以在同一个范围内对相同的函数名有多个定义。函数的定义必须彼此不同,可以是参数列表中的参数类型不同,也可以是参数个数不同。不能重载只有返回类型不同的函数声明。

下面的实例演示了几个相同的函数 print(),用于打印不同的数据类型:

  1. using System;
  2. namespace PolymorphismApplication
  3. {
  4. class Printdata
  5. {
  6. void print(int i)
  7. {
  8. Console.WriteLine("Printing int: {0}", i );
  9. }
  10.  
  11. void print(double f)
  12. {
  13. Console.WriteLine("Printing float: {0}" , f);
  14. }
  15.  
  16. void print(string s)
  17. {
  18. Console.WriteLine("Printing string: {0}", s);
  19. }
  20. static void Main(string[] args)
  21. {
  22. Printdata p = new Printdata();
  23. // 调用 print 来打印整数
  24. p.print(5);
  25. // 调用 print 来打印浮点数
  26. p.print(500.263);
  27. // 调用 print 来打印字符串
  28. p.print("Hello C#");
  29. Console.ReadKey();
  30. }
  31. }
  32. }

我来试一下

当上面的代码被编译和执行时,它会产生下列结果:

Printing int: 5
Printing float: 500.263
Printing string: Hello C#

动态多态性

C# 允许您使用关键字 abstract 创建抽象类,用于提供接口的部分类的实现。当一个派生类继承自该抽象类时,该功能得以实现。抽象类包含抽象方法,抽象方法可被派生类实现。派生类具有更专业的功能。

请注意,下面是有关抽象类的一些规则:

  • 您不能创建一个抽象类的实例。
  • 您不能在一个抽象类外部声明一个抽象方法。
  • 通过在类定义前面放置关键字 sealed,可以将类声明为密封类。当一个类被声明为 sealed 时,它不能被继承。抽象类不能被声明为 sealed,因为抽象类必须被继承才能实现其功能。

下面的程序演示了一个抽象类:

  1. using System;
  2. namespace PolymorphismApplication
  3. {
  4. abstract class Shape
  5. {
  6. public abstract int area();
  7. }
  8. class Rectangle: Shape
  9. {
  10. private int length;
  11. private int width;
  12. public Rectangle( int a=0, int b=0)
  13. {
  14. length = a;
  15. width = b;
  16. }
  17. public override int area ()
  18. {
  19. Console.WriteLine("Rectangle 类的面积:");
  20. return (width * length);
  21. }
  22. }
  23.  
  24. class RectangleTester
  25. {
  26. static void Main(string[] args)
  27. {
  28. Rectangle r = new Rectangle(10, 7);
  29. double a = r.area();
  30. Console.WriteLine("面积: {0}",a);
  31. Console.ReadKey();
  32. }
  33. }
  34. }

我来试一下

当上面的代码被编译和执行时,它会产生下列结果:

Rectangle 类的面积:
面积: 70

当有一个定义在类中的函数需要在继承类中实现时,可以使用虚方法。虚方法是使用关键字 virtual 声明的。虚方法可以在不同的继承类中有不同的实现。对虚方法的调用是在运行时发生的。

动态多态性是通过 抽象类虚方法 实现的。

下面的程序演示了这点:

  1. using System;
  2. namespace PolymorphismApplication
  3. {
  4. class Shape
  5. {
  6. protected int width, height;
  7. public Shape( int a=0, int b=0)
  8. {
  9. width = a;
  10. height = b;
  11. }
  12. public virtual int area()
  13. {
  14. Console.WriteLine("父类的面积:");
  15. return 0;
  16. }
  17. }
  18. class Rectangle: Shape
  19. {
  20. public Rectangle( int a=0, int b=0): base(a, b)
  21. {
  22.  
  23. }
  24. public override int area ()
  25. {
  26. Console.WriteLine("Rectangle 类的面积:");
  27. return (width * height);
  28. }
  29. }
  30. class Triangle: Shape
  31. {
  32. public Triangle(int a = 0, int b = 0): base(a, b)
  33. {
  34. }
  35. public override int area()
  36. {
  37. Console.WriteLine("Triangle 类的面积:");
  38. return (width * height / 2);
  39. }
  40. }
  41. class Caller
  42. {
  43. public void CallArea(Shape sh)
  44. {
  45. int a;
  46. a = sh.area();
  47. Console.WriteLine("面积: {0}", a);
  48. }
  49. }
  50. class Tester
  51. {
  52. static void Main(string[] args)
  53. {
  54. Caller c = new Caller();
  55. Rectangle r = new Rectangle(10, 7);
  56. Triangle t = new Triangle(10, 5);
  57. c.CallArea(r);
  58. c.CallArea(t);
  59. Console.ReadKey();
  60. }
  61. }
  62. }

我来试一下

当上面的代码被编译和执行时,它会产生下列结果:

Rectangle 类的面积:
面积:70
Triangle 类的面积:
面积:25
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号