自动使基构造函数在派生类中可用

自动使基构造函数在派生类中可用

本文介绍了自动使基构造函数在派生类中可用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个构造函数的基类,需要一个参数:

I have a Base Class with two constructors, requiring a parameter:

public abstract class StoreBase
{
    private readonly SomeObject_sobj;

    protected StoreBase(SomeObject sobj)
    {
        _sobj = sobj;
    }

    protected StoreBase(OtherObject oobj)
    {
        _sobj = new SomeObject(oobj);
    }
}

然后我有一个派生类:

public class MyDerived: StoreBase
{

}

这会导致编译错误,因为基类不包含无参数构造函数.

This causes a compilation error as base class doesn't contain parameterless constructor.

我的理解是,因为 MyDerived 不包含构造函数,所以编译器添加了一个无参数构造函数(这是众所周知的,与派生类无关).但是,由于它派生自另一个类,因此需要先运行基类构造函数,无法从空的 MyDerived 构造函数中确定应该运行哪个构造函数.

My understanding is that because MyDerived doesn't contain a constructor, the compiler adds a parameterless constructor (that's well known and nothing to do with derived classes). However, as it derives from another class, the base class constructor needs to run first, and there is no way to determine which constructor should run from the empty MyDerived constructor.

基本上我在问:如果我真的不需要额外的构造函数逻辑,我可以避免将所有构造函数从 Base 复制/粘贴到派生类吗?我可以说从基础中获取所有构造函数"而不将它们全部添加吗?

Basically I'm asking: can I avoid copy/pasting all constructors from Base into Derived class if I really don't need additional constructor logic? Can I say "Take all constructors from base" without adding them all?

(是的,我知道我可以/应该将其重构为无参数构造函数和受保护的虚拟 Initialize() 方法.但我仍然想知道我是否可以使用构造函数并仍然避免复制/粘贴)

(And yes, I know I could/should refactor this into a parameterless constructor and a protected virtual Initialize() method. but I still wonder if I can work with constructors and still avoid copy/paste)

推荐答案

否 - 您还需要在派生类中实现(适当的)构造函数.

No - you will need to implement the (appropriate) constructors in the derived class, as well.

派生类只需要使用基类构造函数之一——所以其中需要的构造函数可能与基类完全不同.它们需要手动实现,即使只是:

The derived class only needs to use one of the base constructors - so the constructors required in it may be completely different than the base class. They will need to be implemented by hand, even if that's just:

public class MyDerived : StoreBase
{
     public MyDerived(SomeObject sobj) : base(sobj) {}
     public MyDerived(OtherObject  oobj) : base(oobj) {}
}

还有:

(是的,我知道我可以/应该将其重构为无参数构造函数和受保护的虚拟 Initialize() 方法.但我仍然想知道我是否可以使用构造函数并仍然避免复制/粘贴)

虽然我看到这被吹捧,但我相信这并不总是一个好的做法.在许多情况下,这实际上是有问题的,因为如果子类覆盖了受保护的虚拟方法,您将依赖子类正确调用 Initialize.例如,如果子类这样做,它可能会非常糟糕:

Although I see this touted, I believe this is not always a good practice. In many cases, this is actually problematic, as you're relying on the subclass to properly call Initialize if they override your protected virtual method. For example, if the subclass did this, it could potentially be very bad:

public class MyDerived : StoreBase
{
   // .. other stuff
   protected override void Initialize()
   {
       // Leave out, intentionally or accidentally, the following:
       // base.Initialize();
   }
}

我实际上在大多数情况下都会避免这种情况,并在构造函数中进行初始化(或在私有的非虚拟初始化方法中).不这样做会破坏您对初始化将始终按预期方式进行的任何保证.

I actually avoid this in most situations, and initialize in the constructors (or in a private, non-virtual initialize method). Not doing this breaks any guarantees you have that your initialization will always occur the way you intend.

构造函数和构造函数链提供相同的功能,但有更好的保证.

Constructors and constructor chaining provide the same functionality, with much better guarantees.

这篇关于自动使基构造函数在派生类中可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 09:13