简体   繁体   English

Abstract类的子类,总是调用Abstract构造函数

[英]Subclass of an Abstract class, always call Abstract constructor

I have an abstract class and a child class 我有一个抽象类和一个子类

abstract class abstractClass
{
    public int x;

    protected abstractClass(int x)
    {
        this.x = x;
    }
}

class childClass : abstractClass
{
    childClass(int x)
        : base(x)
    {
    }
}

I have many child classes like this one, where all of those classes always just call the common constructor abstractClass1(int x) by calling base(x) in their constructors. 我有很多像这样的子类,其中所有这些类总是通过在它们的构造函数中调用base(x)来调用公共构造函数abstractClass1(int x)

When I want to add a new property to the constructor, for example another int y , I have to edit all of those classes and add the y as a parameter in every child's constructor plus the abstract class constructor. 当我想向构造函数添加一个新属性时,例如另一个int y ,我必须编辑所有这些类,并将y作为参数添加到每个子构造函数和抽象类构造函数中。

Is there another way of doing this, considering I know beforehand that every child class will be using the abstract class constructor and won't be adding anything else ? 有没有另一种方法可以做到这一点,考虑到我事先知道每个子类都将使用抽象类构造函数而不会添加任何其他内容?

Here's one idea: make an AbstractClassParameters class: 这是一个想法:创建一个AbstractClassParameters类:

class AbstractClassParameters {
    public int x { get; private set; }
    // add more parameters here
    public AbstractClassParameters(int x) {
        this.x = x;
        // add more initializers here
    }
}

Now you can pass an instance of AbstractClassParameters to the abstract class constructor, and every subclass can do the same, just passing it through. 现在,您可以将AbstractClassParameters的实例传递给抽象类构造函数,并且每个子类都可以执行相同的操作,只需将其传递通过即可。

abstract class abstractClass
{
    public int x;

    protected abstractClass(AbstractClassParameters p)
    {
        this.x = p.x;            
    }
}

class childClass : abstractClass
{
    childClass(AbstractClassParameters p) : base(p) { }
}

When you add a parameter, you only have to edit the AbstractClassParameters class and the abstractClass , not every subclass. 添加参数时,只需编辑AbstractClassParameters类和abstractClass ,而不是每个子类。

Unfortunately I don't think you can do it any other way. 不幸的是,我认为你不能以任何其他方式做到这一点。 You have to modify abstract class because you have to add new member y. 您必须修改抽象类,因为您必须添加新成员y。 You either modify the abstract class constructor to initialize the member y or you can create a property around y and let children initialize it inside the constructor. 您可以修改抽象类构造函数来初始化成员y,也可以在y周围创建一个属性,让子元素在构造函数中初始化它。

It seems to me that most of the already existing sub-classes can easily accept a default value for the new property y . 在我看来,大多数已经存在的子类可以很容易地接受新属性y的默认值。 In this case add a new constructor for the base class with the new property initializer, and have the old constructor set the value of the new property to the common default value. 在这种情况下,使用新属性初始值设定项为基类添加新的构造函数,并让旧构造函数将新属性的值设置为公共默认值。 Now only the sub-classes that require a non-default value for the new property need to call the extended base-class constructor. 现在,只有需要新属性的非默认值的子类才需要调用扩展的基类构造函数。

Your example becomes: 你的例子变成:

abstract class abstractClass
{
    public int x { get; private set; }
    public int y { get; private set; }

    protected abstractClass(int x) : this(x, defaultY) {}
    protected abstractClass(int x, int y)
    {
        this.x = x;
        this.y = y;
    }
}

class childClass : abstractClass
{
    childClass(int x)
        : base(x)
    {
    }
}

class childClass2 : abstractClass
{
    childClass(int x, int y)
        : base(x,y)
    {
    }
}

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

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