继承

基类和派生类

一个类可以派生自多个类或接口,这意味着它可以从多个基类或接口继承数据和函数。
C# 中创建派生类的语法如下:

  1. <访问修饰符符> class <基类>
  2. {
  3. ...
  4. }
  5. class <派生类> : <基类>
  6. {
  7. ...
  8. }

class Rectangle :Shape

多态性

静态多态
您可以在同一个范围内对相同的函数名有多个定义。函数的定义必须彼此不同,可以是参数列表中的参数类型不同,也可以是参数个数不同。不能重载只有返回类型不同的函数声明。
下面的实例演示了几个相同的函数 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. 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. }

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

  1. Printing int: 5
  2. Printing float: 500.263
  3. Printing string: Hello C++

动态多态
先用abstract 定一个抽象类以及抽象类方法,然后在派生类里override方法

  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. class RectangleTester
  24. {
  25. static void Main(string[] args)
  26. {
  27. Rectangle r = new Rectangle(10, 7);
  28. double a = r.area();
  29. Console.WriteLine("面积: {0}",a);
  30. Console.ReadKey();
  31. }
  32. }
  33. }

先用virtual 定一个虚方法,然后在派生类里override该方法

  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. public override int area ()
  24. {
  25. Console.WriteLine("Rectangle 类的面积:");
  26. return (width * height);
  27. }
  28. }
  29. class Triangle: Shape
  30. {
  31. public Triangle(int a = 0, int b = 0): base(a, b)
  32. {
  33. }
  34. public override int area()
  35. {
  36. Console.WriteLine("Triangle 类的面积:");
  37. return (width * height / 2);
  38. }
  39. }
  40. class Caller
  41. {
  42. public void CallArea(Shape sh)
  43. {
  44. int a;
  45. a = sh.area();
  46. Console.WriteLine("面积: {0}", a);
  47. }
  48. }
  49. class Tester
  50. {
  51. static void Main(string[] args)
  52. {
  53. Caller c = new Caller();
  54. Rectangle r = new Rectangle(10, 7);
  55. Triangle t = new Triangle(10, 5);
  56. c.CallArea(r);
  57. c.CallArea(t);
  58. Console.ReadKey();
  59. }
  60. }
  61. }

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

运算符重载

重载运算符是具有特殊名称的函数,是通过关键字 operator 后跟运算符的符号来定义的。与其他函数一样,重载运算符有返回类型和参数列表。
例如,下面的函数,以自定义的box函数(盒子,函数有盒子的长宽高对象,以及求体积方法)为例,重载了加法运算。返回一个box对象:

  1. public static Box operator+ (Box b, Box c)
  2. {
  3. Box box = new Box();
  4. box.length = b.length + c.length;
  5. box.breadth = b.breadth + c.breadth;
  6. box.height = b.height + c.height;
  7. return box;
  8. }

上面的函数为用户自定义的类 Box 实现了加法运算符(+)。它把两个 Box 对象的属性相加,并返回相加后的 Box 对象。

接口(Interface)

public
接口定义了属性、方法和事件,这些都是接口的成员。接口只包含了成员的声明。成员的定义是派生类的责任。接口提供了派生类应遵循的标准结构。接口的实现与类的继承语法格式类似
接口比类的优势在于类在重载时比较复杂,接口从开始只定义了接口,每一个实例都必须描述方法下的具体动作,不需要“重写”操作。