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

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

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

    假设,有一个基类 ,它的派生类是 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("总面积: {0}", Rect.getArea());
    34. Console.ReadKey();
    35. }
    36. }
    37. }

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

    派生类继承了基类的成员变量和成员方法。因此父类对象应在子类对象创建之前被创建。您可以在成员初始化列表中进行父类的初始化。

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

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

    多重继承指的是一个类别可以同时从多于一个父类继承行为与特征的功能。与单一继承相对,单一继承指一个类别只可以继承自一个父类。

    C# 不支持多重继承。但是,您可以使用接口来实现多重继承。下面的程序演示了这点:

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

    🔚