📜  C#|多级继承

📅  最后修改于: 2021-05-29 14:52:21             🧑  作者: Mango

在多级继承中,派生类将继承基类,并且派生类也将充当其他类的基类。例如,如下图所示,称为A,B和C的三个类,其中C类是从B类派生的,而B类是从A类派生的。在这种情况下,每个派生的类都继承了A,B和C的所有特征。它的基类。因此,类C继承了类A和B的所有功能。

示例:在这里,派生类Rectangle用作创建派生类ColorRectangle的基类。由于继承,ColorRectangle继承了Rectangle和Shape的所有特征,并添加了一个名为rcolor的额外字段,其中包含矩形的颜色。

此示例还涵盖了派生类中的构造函数的概念。众所周知,子类继承其父类的所有成员(字段,方法),但构造函数不是成员,因此子类不会继承它们,但是可以从子类调用父类的构造函数。如下例所示,base指的是最接近的基类中的构造函数。 ColorRectangle中的基础在Rectangle中调用构造函数,而Rectangle类中的基础在Shape中调用构造函数。

// C# program to illustrate the
// concept of multilevel inheritance
using System;
  
class Shape {
  
    double a_width;
    double a_length;
  
    // Default constructor
    public Shape()
    {
        Width = Length = 0.0;
    }
  
    // Constructor for Shape
    public Shape(double w, double l)
    {
        Width = w;
        Length = l;
    }
  
    // Construct an object with 
    // equal length and width
    public Shape(double y)
    {
        Width = Length = y;
    }
  
    // Properties for Length and Width
    public double Width
    {
        get {
               return a_width; 
            }
  
        set { 
              a_width = value < 0 ? -value : value; 
            }
    }
  
    public double Length
    {
        get { 
               return a_length; 
            }
  
        set { 
              a_length = value < 0 ? -value : value;
            }
    }
    public void DisplayDim()
    {
        Console.WriteLine("Width and Length are " 
                     + Width + " and " + Length);
    }
}
  
// A derived class of Shape 
// for the rectangle.
class Rectangle : Shape {
  
    string Style;
  
    // A default constructor. 
    // This invokes the default
    // constructor of Shape.
    public Rectangle()
    {
        Style = "null";
    }
  
    // Constructor
    public Rectangle(string s, double w, double l)
        : base(w, l)
    {
        Style = s;
    }
  
    // Construct an square.
    public Rectangle(double y)
        : base(y)
    {
        Style = "square";
    }
  
    // Return area of rectangle.
    public double Area()
    {
        return Width * Length;
    }
  
    // Display a rectangle's style.
    public void DisplayStyle()
    {
        Console.WriteLine("Rectangle is  " + Style);
    }
}
  
// Inheriting Rectangle class
class ColorRectangle : Rectangle {
  
    string rcolor;
  
    // Constructor
    public ColorRectangle(string c, string s,
                          double w, double l)
        : base(s, w, l)
    {
        rcolor = c;
    }
  
    // Display the color.
    public void DisplayColor()
    {
        Console.WriteLine("Color is " + rcolor);
    }
}
  
// Driver Class
class GFG {
  
    // Main Method
    static void Main()
    {
        ColorRectangle r1 = new ColorRectangle("pink", 
                   "Fibonacci rectangle", 2.0, 3.236);
  
        ColorRectangle r2 = new ColorRectangle("black",
                                   "Square", 4.0, 4.0);
  
        Console.WriteLine("Details of r1: ");
        r1.DisplayStyle();
        r1.DisplayDim();
        r1.DisplayColor();
  
        Console.WriteLine("Area is " + r1.Area());
        Console.WriteLine();
  
        Console.WriteLine("Details of r2: ");
        r2.DisplayStyle();
        r2.DisplayDim();
        r2.DisplayColor();
  
        Console.WriteLine("Area is " + r2.Area());
    }
}
输出:
Details of r1: 
Rectangle is  Fibonacci rectangle
Width and Length are 2 and 3.236
Color is pink
Area is 6.472

Details of r2: 
Rectangle is  Square
Width and Length are 4 and 4
Color is black
Area is 16