在C#中,基类和派生类都可以具有自己的构造函数。用于实例化基类对象的基类的构造函数和用于实例化派生类对象的派生类的构造函数。在继承中,派生类继承基类的所有成员(字段,方法),但是派生类不能继承基类的构造函数,因为构造函数不是该类的成员。代替由派生类继承构造函数,只允许调用基类的构造函数。
在C#中,当我们在继承中使用构造函数时,会出现两种不同的情况,如下所示:
情况1:在这种情况下,仅派生类包含一个构造函数。因此,派生类的对象由该构造函数实例化,而基类的对象由默认构造函数自动实例化。
例子:
// C# program to illustrate the
// concept of inheritance in the
// constructor when the derived
// class contains a constructor
using System;
// Class Tank to give the
// dimension of the tank
class Tank {
double t_radius;
double t_height;
// Properties for Radius and Height
public double Radius
{
get {
return t_radius;
}
set {
t_radius = value < 0 ? -value : value;
}
}
public double Height
{
get {
return t_height;
}
set {
t_height = value < 0 ? -value : value;
}
}
// Display the dimension of tanks
public void DisplayDimension()
{
Console.WriteLine("The radius of tank is :" + Radius
+ " and the height of tank is :" + Height);
}
}
// A derived class AreaOfTank
// inheriting Tank Class
class AreaOfTank : Tank {
string Color;
// Constructor
public AreaOfTank(string c, double r, double h)
{
// from base class
Radius = r;
Height = h;
// from derived class
Color = c;
}
// Return area of tank
public double Area()
{
return 2 * 3.14 * Radius * Height;
}
// Display the color of tank
public void DisplayColor()
{
Console.WriteLine("The Color of tank is "
+ Color);
}
}
// Driver Class
class GFG {
// Main Method
static void Main()
{
// Create and initialize the
// object of AreaOfTank
AreaOfTank t1 = new AreaOfTank("Green", 6.0, 12.0);
t1.DisplayColor();
t1.DisplayDimension();
Console.WriteLine("Area is " + t1.Area());
}
}
The Color of tank is Green
The radius of tank is :6 and the height of tank is :12
Area is 452.16
说明:在以上示例中, Tank是基类,而AreaOfTank是派生类。容器类提供了容器的尺寸,而AreaOfTank提供了容器的颜色和面积。并且Tank类不包含任何构造函数,因此默认构造函数用于实例化类的对象,而AreaOfTank类包含用于实例化类对象的AreaOfTank()构造函数。
情况2:在这种情况下,基类和派生类都具有自己的构造函数,因此过程很复杂,因为必须执行两个类的构造函数。为了克服这种情况,C#提供了一个称为基本关键字的关键字。借助base关键字,派生类可以调用在其基类中定义的构造函数。
注意:基类中定义的任何形式的构造函数都可以由base关键字调用,但是只有执行与参数匹配的构造函数。
句法:
derived-constructor(parameter-list) : base(argument-list)
{
// body of constructor
}
在这里,arguments-list包含基类的构造函数所需的参数。
例子:
// C# program to illustrate the concept of
// inheritance in constructors when both
// the base class and derived class
// their own constructors
using System;
// Class Tank to give the
// dimension of the tank
class Tank {
double t_radius;
double t_height;
// Constructor for Tank
public Tank(double r, double h)
{
Radius = r;
Height = h;
}
// Properties for Radius
// and Height
public double Radius
{
get {
return t_radius;
}
set {
t_radius = value < 0 ? -value : value;
}
}
public double Height
{
get {
return t_height;
}
set {
t_height = value < 0 ? -value : value;
}
}
// Display the dimension of tanks
public void DisplayDimension()
{
Console.WriteLine("The radius of tank is :" + Radius
+ " and the height of tank is :" + Height);
}
}
// AreaOfTank is derived class
// which is inheriting the Tank class
class AreaOfTank : Tank {
string Color;
// Call the Constructor of the
// base class, i.e Tank
// Using base keyword
public AreaOfTank(string c, double r,
double h) : base(r, h)
{
Color = c;
}
// Return area of tank
public double Area()
{
return 2 * 3.14 * Radius * Height;
}
// Display the color of tank
public void DisplayColor()
{
Console.WriteLine("The Color of tank is " + Color);
}
}
// Driver Class
class GFG {
// Main Method
static void Main()
{
// Create and initialize the
// object of AreaOfTank
AreaOfTank t1 = new AreaOfTank("Brown", 4.0, 8.0);
t1.DisplayColor();
t1.DisplayDimension();
Console.WriteLine("Area is " + t1.Area());
}
}
The Color of tank is Brown
The radius of tank is :4 and the height of tank is :8
Area is 200.96
说明:在上面的示例中, Tank是基类, AreaOfTank是派生类。 Tank类描述了容器的尺寸,AreaOfTank描述了容器的颜色和面积。基类和派生类都有自己的构造函数。但是我们使用基本关键字声明AreaOfTank的构造函数,如下所示:
public AreaOfTank(string c, double r, double h) : base (r, h)
{
Color = c;
}
在这里AreaOfTank()使用参数r和h调用基类构造函数。这意味着将调用Tank()构造函数,它将在AreaOfTank()中初始化Radius和Height的值。因此,不需要AreaOfTank类来初始化这些值。如果AreaOfTank需要一个额外的字段,则该字段应与“颜色”之类的被调用字段唯一。
通过使用base关键字,可以更轻松地初始化基类的对象而不会发生任何冲突,它还提供了从派生类中调用基类的构造函数的权限,还节省了重写代码的时间。