自动使基础构造函数在派生类中可用? [英] Automatically making Base Constructors available in derived class?

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

问题描述

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

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复制/粘贴到Derived类中?我可以说从基础结构中提取所有构造函数"而不添加全部吗?

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()方法.但是我仍然想知道是否可以使用构造函数并且仍然避免复制/粘贴)

(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)

尽管我看到了这种吹捧,但我相信这并不总是一种好的做法.在许多情况下,这实际上是有问题的,因为如果子类覆盖了受保护的虚拟方法,则您依赖子类来正确调用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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆