课程表

C# 基础教程

C# 高级教程

C# 工具/手册

工具箱
速查手册

C# 继承

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

继承是面向对象程序设计中最重要的概念之一。继承允许我们根据一个类来定义另一个类来定义一个类,这使得创建和维护应用程序变得更容易。同时也有利于重用代码和节省开发时间。

当创建一个类时,程序员不需要完全重新编写新的数据成员和成员函数,只需要设计一个新的类,继承了已有的类的成员即可。这个已有的类被称为的基类,这个新的类被称为派生类

继承的思想实现了 属于(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. // 派生类
  19. class Rectangle: Shape
  20. {
  21. public int getArea()
  22. {
  23. return (width * height);
  24. }
  25. }
  26. class RectangleTester
  27. {
  28. static void Main(string[] args)
  29. {
  30. Rectangle Rect = new Rectangle();
  31.  
  32. Rect.setWidth(5);
  33. Rect.setHeight(7);
  34.  
  35. // 打印对象的面积
  36. Console.WriteLine("总面积: {0}", Rect.getArea());
  37. Console.ReadKey();
  38. }
  39. }
  40. }

我来试一下

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

总面积: 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("长度: {0}", length);
  21. Console.WriteLine("宽度: {0}", width);
  22. Console.WriteLine("面积: {0}", GetArea());
  23. }
  24. }//end class 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("成本: {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. }

我来试一下

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

长度: 4.5
宽度: 7.5
面积: 33.75
成本: 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.  
  18. // 基类 PaintCost
  19. public interface PaintCost
  20. {
  21. int getCost(int area);
  22.  
  23. }
  24. // 派生类
  25. class Rectangle : Shape, PaintCost
  26. {
  27. public int getArea()
  28. {
  29. return (width * height);
  30. }
  31. public int getCost(int area)
  32. {
  33. return area * 70;
  34. }
  35. }
  36. class RectangleTester
  37. {
  38. static void Main(string[] args)
  39. {
  40. Rectangle Rect = new Rectangle();
  41. int area;
  42. Rect.setWidth(5);
  43. Rect.setHeight(7);
  44. area = Rect.getArea();
  45. // 打印对象的面积
  46. Console.WriteLine("总面积: {0}", Rect.getArea());
  47. Console.WriteLine("油漆总成本: ${0}" , Rect.getCost(area));
  48. Console.ReadKey();
  49. }
  50. }
  51. }

我来试一下

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

总面积: 35
油漆总成本: $2450
转载本站内容时,请务必注明来自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号