继承

面向对象程序设计中最重要的一个概念就是继承(inheritance)。继承允许我们在另一个类中定义一个新的类,这使得它更容易创建和维护一个应用程序。这也提供了一个机会来重用代码的功能,加快实现时间。

创建一个类的时候,不是要写全新的数据成员和成员函数,程序员可以指定新的类继承一个已经存在的类的成员。已有的类称为基类(base class),新的类称为派生类(derived class)

继承的思想实现了 IS-A 的关系。例如,哺乳动物是(IS-A)动物,狗是(IS-A)哺乳动物,因此狗是(IS-A)一个动物等。

基类和派生类

一个类可以从多个类或接口被派生,这意味着它可以从多个基类或接口继承数据和函数。

用 C# 创建派生类的语法如下:

  1. <acess-specifier> class <base_class>
  2. {
  3. ...
  4. }
  5. class <derived_class> : <base_class>
  6. {
  7. ...
  8. }

比如基类为 Shape ,其派生类为 Rectangle :

  1. using System;
  2. namespace InheritanceApplication
  3. {
  4. class Shape
  5. {
  6. public void setWidth(int w)
  7. {
  8. width = w;
  9. }
  10. public void setHeight(int h)
  11. {
  12. height = h;
  13. }
  14. protected int width;
  15. protected int height;
  16. }
  17. // 派生类
  18. class Rectangle: Shape
  19. {
  20. public int getArea()
  21. {
  22. return (width * height);
  23. }
  24. }
  25. class RectangleTester
  26. {
  27. static void Main(string[] args)
  28. {
  29. Rectangle Rect = new Rectangle();
  30. Rect.setWidth(5);
  31. Rect.setHeight(7);
  32. // 打印对象的面积
  33. Console.WriteLine("Total area: {0}", Rect.getArea());
  34. Console.ReadKey();
  35. }
  36. }
  37. }

编译执行上述代码,得到如下结果:

  1. Total area: 35

初始化基类

派生类继承基类的成员变量和成员方法。因此,父类对象应该是先于子类被创建。你可以在初始化列表中说明父类的初始化。

下面的程序论证了上述方法:

  1. using System;
  2. namespace RectangleApplication
  3. {
  4. class Rectangle
  5. {
  6. //成员变量
  7. protected double length;
  8. protected double width;
  9. public Rectangle(double l, double w)
  10. {
  11. length = l;
  12. width = w;
  13. }
  14. public double GetArea()
  15. {
  16. return length * width;
  17. }
  18. public void Display()
  19. {
  20. Console.WriteLine("Length: {0}", length);
  21. Console.WriteLine("Width: {0}", width);
  22. Console.WriteLine("Area: {0}", GetArea());
  23. }
  24. }// Rectangle 类结束
  25. class Tabletop : Rectangle
  26. {
  27. private double cost;
  28. public Tabletop(double l, double w) : base(l, w)
  29. { }
  30. public double GetCost()
  31. {
  32. double cost;
  33. cost = GetArea() * 70;
  34. return cost;
  35. }
  36. public void Display()
  37. {
  38. base.Display();
  39. Console.WriteLine("Cost: {0}", GetCost());
  40. }
  41. }
  42. class ExecuteRectangle
  43. {
  44. static void Main(string[] args)
  45. {
  46. Tabletop t = new Tabletop(4.5, 7.5);
  47. t.Display();
  48. Console.ReadLine();
  49. }
  50. }
  51. }

编译执行上述代码,得到如下结果:

  1. Length: 4.5
  2. Width: 7.5
  3. Area: 33.75
  4. Cost: 2362.5

C# 中的多重继承

C# 不支持多重继承。但是你可以使用接口来实现多重继承。下面的程序实现了该功能:

  1. using System;
  2. namespace InheritanceApplication
  3. {
  4. class Shape
  5. {
  6. public void setWidth(int w)
  7. {
  8. width = w;
  9. }
  10. public void setHeight(int h)
  11. {
  12. height = h;
  13. }
  14. protected int width;
  15. protected int height;
  16. }
  17. // 基类 PaintCost
  18. public interface PaintCost
  19. {
  20. int getCost(int area);
  21. }
  22. // 派生类
  23. class Rectangle : Shape, PaintCost
  24. {
  25. public int getArea()
  26. {
  27. return (width * height);
  28. }
  29. public int getCost(int area)
  30. {
  31. return area * 70;
  32. }
  33. }
  34. class RectangleTester
  35. {
  36. static void Main(string[] args)
  37. {
  38. Rectangle Rect = new Rectangle();
  39. int area;
  40. Rect.setWidth(5);
  41. Rect.setHeight(7);
  42. area = Rect.getArea();
  43. //打印对象面积
  44. Console.WriteLine("Total area: {0}", Rect.getArea());
  45. Console.WriteLine("Total paint cost: ${0}" , Rect.getCost(area));
  46. Console.ReadKey();
  47. }
  48. }
  49. }

编译执行上述代码,得到如下结果:

  1. Total area: 35
  2. Total paint cost: $2450