简体   繁体   English

将继承的类构造函数参数传递给基本构造函数参数

[英]Pass Inherited Class Constructor Arguments to Base Constructor Arguments

What is the "C# way" to pass arguments of inherited class constructors to base class constructors? 将继承的类构造函数的参数传递基类构造函数的“ C#方法”是什么? Would this require that I override the base_model string in the base class? 这会要求我重写基类中的base_model字符串吗?

// Base class
class vehicle
{
    private string base_model = String.Empty;

    vehicle(string model)
    {
        this.base_model = model;
    }

}


// Inherited classes
class tesla : vehicle
{
    private readonly string model = "Model S";      // This is unchanging

    tesla(string model) : base ( **pass model to base_model** )
    {
        // Initialize
    }
}

class ferrari : vehicle
{
    private readonly string model = "458 Spider";      // This is unchanging

    ferrari(string model) : base ( **pass model to base_model** )
    {
        // Initialize
    }

}

Edit 编辑

@JohnLBevan brought another question to my attention: @JohnLBevan引起了我另一个注意:

This brings to mind another question: is it possible to have no constructor arguments for the tesla class (or the ferrari class for that matter) and automatically pass the model variable (ie "Model S" ) to the base_model ? 这使我想到另一个问题:是否可能没有tesla类(或ferrari类的构造函数)参数,并自动将model变量(即"Model S" )传递给base_model

A far more OO standards compliant way to do this is to use an abstract property. 实现此目标的一种更符合OO标准的方法是使用抽象属性。 This means the base class has defined the property but the extending classes must implement it. 这意味着基类已经定义了属性,但是扩展类必须实现它。 This also means you cannot directly create an instance of vehicle , instead you must create one of the derived types. 这也意味着您不能直接创建vehicle的实例,而必须创建派生类型之一。

abstract class vehicle
{

    vehicle() {   }

    public abstract string Model { get; }

}


// Inherited classes
class tesla : vehicle
{
    private readonly string model = "Model S";      // This is unchanging

    public override string Model { get { return model; }}
}

class ferrari : vehicle
{

    public override string Model { get { return "458 Spider"; }}

}

The only time you really want to be passing values from derived constructors to base constructors is when those values are actively used in the base class. 您真正想要将值从派生构造函数传递给基构造函数的唯一时间是在基类中积极使用这些值时。 This is a normally when you've overridden a base constructor, or you are using a dependency injection or dependency resolution pattern. 通常,当您重写基本构造函数或使用依赖项注入或依赖项解决方案模式时,通常会出现这种情况。 The base class should not act as an aggregation of information from the derived class. 基类不应充当派生类中信息的集合。

Exactly what you've done, only putting the variable name in where you had the asterisks: 正是您所做的,仅将变量名放在带有星号的位置:

// Inherited classes    
class tesla : vehicle
{
    private readonly string model = "Model S";      // This is unchanging

    tesla(string model) : base ( model )
    {
        // Initialize
    }
}

You also need to ensure that the base class's constructor is visible to its subclasses (ie is not private) 您还需要确保基类的构造函数对其子类可见(即不是私有的)

// Base class
class vehicle
{
    private string base_model = String.Empty;

    protected vehicle(string model)
    {
        this.base_model = model;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM