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

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

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

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

    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. void print(double f)
    11. {
    12. Console.WriteLine("Printing float: {0}" , f);
    13. }
    14. void print(string s)
    15. {
    16. Console.WriteLine("Printing string: {0}", s);
    17. }
    18. static void Main(string[] args)
    19. {
    20. Printdata p = new Printdata();
    21. // 调用 print 来打印整数
    22. p.print(5);
    23. // 调用 print 来打印浮点数
    24. p.print(500.263);
    25. // 调用 print 来打印字符串
    26. p.print("Hello C++");
    27. Console.ReadKey();
    28. }
    29. }
    30. }

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

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

    • 您不能创建一个抽象类的实例。

    • 您不能在一个抽象类外部声明一个抽象方法。

    • 通过在类定义前面放置关键字 sealed,可以将类声明为密封类。当一个类被声明为 sealed 时,它不能被继承。抽象类不能被声明为 sealed。

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

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

    当有一个定义在类中的函数需要在继承类中实现时,可以使用虚方法。虚方法是使用关键字 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. {
    19. {
    20. }
    21. public override int area ()
    22. {
    23. Console.WriteLine("Rectangle 类的面积:");
    24. return (width * height);
    25. }
    26. }
    27. class Triangle: Shape
    28. {
    29. public Triangle(int a = 0, int b = 0): base(a, b)
    30. {
    31. }
    32. public override int area()
    33. {
    34. Console.WriteLine("Triangle 类的面积:");
    35. return (width * height / 2);
    36. }
    37. }
    38. class Caller
    39. {
    40. public void CallArea(Shape sh)
    41. {
    42. int a;
    43. a = sh.area();
    44. Console.WriteLine("面积: {0}", a);
    45. }
    46. }
    47. class Tester
    48. {
    49. static void Main(string[] args)
    50. {
    51. Caller c = new Caller();
    52. Rectangle r = new Rectangle(10, 7);
    53. Triangle t = new Triangle(10, 5);
    54. c.CallArea(r);
    55. c.CallArea(t);
    56. Console.ReadKey();
    57. }
    58. }

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

    🔚