将参数隐式传递给基本构造函数 [英] Implicitly passing parameter to base constructor

查看:101
本文介绍了将参数隐式传递给基本构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的课:

I have a class which looks like:

public class WidgetDao : AbstractDao<Widget>, IWidgetDao
{
    public WidgetDao(ISession session)
        : base (session)
    {

    }
}

如果我尝试在类似以下代码的代码上删除此构造函数,则我的代码会生成错误:

My code has build errors if I attempt to remove this constructor on code which looks like:

public IWidgetDao GetWidgetDao()
{
    return _widgetDao ?? (_widgetDao = new WidgetDao(_session));
}

我有点理解为什么会出现构建错误...没有使用一个参数的显式 WidgetDao 构造函数。但是, WidgetDao 继承自 AbstractDao< Widget> ,后者确实具有采用一个参数的构造函数。

I sort of understand the reason why I get a build error... there's no explicit WidgetDao constructor which takes one parameter. However, WidgetDao inherits from AbstractDao<Widget> which does have a constructor which takes one parameter.

我不明白为什么C#编译器会在 WidgetDao 上使用显式的构造函数来传递显式构造函数

I don't understand why the C# compiler would make an explicit constructor required on WidgetDao when all it's used for is pass-through of the parameter to a base class.

有人可以帮我一下吗?

推荐答案

不幸的是,没有办法使它自动化。 C#将为您的对象提供的唯一隐式构造函数是默认的无参数构造函数,并且仅在代码中没有替代方法时才这样做。稍后,我将在此基础上进行构建,但仅用于基础信息...

Unfortunately there is no way to automate this. The only implicit constructor that C# will give your object is the default parameterless constructor, and it only does that if there are no alternatives in your code. I'll build on this in a moment, but just for the foundation information...

隐式 MyClass()构造函数:

public class MyClass 
{
}

在上述情况下,编译器假定您需要一个公共的无参数构造函数,除了允许您创建对象外,该构造函数实际上没有做任何其他事情。即使您没有创建构造函数,也可以整天调用 new MyClass()

In the above case, the compiler assumed you wanted a public parameterless constructor that didn't really do anything other than allow you to create an object. You can call new MyClass() all day long even though you didn't create a constructor.

现在,创建自己的构造函数时会发生什么?

Now, what happens when you create your own constructor?

public class MyClass
{
    public MyClass(string name)
    {
        Name = name;
    }

    public string Name { get; private set; }
}

在这种情况下,隐式无参数构造函数仍然存在,但是现在它是私有的。这意味着您不能再调用 new MyClass(),并且需要提供一个名称。

In this case, that implicit parameterless constructor still exists, but now it's private. That means you cannot call new MyClass() any longer and you are required to provide a name.

现在让我们使我们的类成为抽象基类:

Now let's make our class an abstract base class:

public abstract class AbstractMyClass
{
    protected AbstractMyClass(string name)
    {
        Name = name;
    }

    public string Name { get; private set; }

    public abstract void DoSomething();
}

无法实例化抽象类,因此这是一种最佳做法您的构造函数受保护的 。即使您将它们设为公开,您也无法直接访问它们。重要的是,同样的警告也适用:默认的无参数构造函数现在是私有的。任何实现此基类的类都必须提供 name 的值,否则该类未完全实例化。

There's no way to instantiate an abstract class, so it's a best practice to make your constructors protected. Even if you made them public you couldn't access them directly anyway. The important thing is that the same caveat holds: the default parameterless constructor is now private. Any class that implements this base class must provide a value for name or the class is not fully instantiated.

public class SeansClass
{
    public SeansClass() : base("Sean") { }

    public void DoSomething() { }
}

在这种情况下,我们重新引入了标准的无参数构造函数,并提供了名称。这是完全有效的代码。 唯一的警告是, base()调用中提供的值必须是编译时间常数参数传递给您的类。始终首先评估 base()方法。

In this case, we re-introduced the standard parameterless constructor and had it provide the name for our class. This is perfectly valid code. The only caveat is that the value provided in the base() call must be a compile time constant or calculated from the parameter passed in to your class. The base() method is always evaluated first.

C#无法假设您实际上希望基类的所有实现都具有相同的构造函数。每个类都可以添加构造函数或为基类提供自己的值。这只是C#的局限性。

There is no way for C# to make the assumption that you in fact want all implementations of a base class to have the same constructor. Each class may add constructors or provide their own values for the base class. This is merely a limitation of C#.

这篇关于将参数隐式传递给基本构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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