C# 中的 this() 和 base() 构造函数

发布于 2024-10-15 10:23:30 字数 702 浏览 2 评论 0原文

似乎没有语言语法可以同时指定 this() 和 base() 构造函数。给出以下代码:

public class Bar : Foo
{
    public Bar()
      :base(1)
      //:this(0)
    { }
    public Bar(int schmalue)
      :Foo(1)
    {
       //Gobs of initialization code
       ...
       Schmalue = schmalue;
    }

    public int Schmalue { get; private set; }
}

public class Foo
{
    public Foo()
    {
        Value = 0;
    }

    public class Foo(int value)
    {
        Value = value;
    }

    public int Value { get; private set; }
}

编译器给我一个错误,指出取消注释 :this(0) 调用时需要 '{'。这很麻烦,因为它导致我将代码分解为私有方法,而明确提供此功能是为了防止这种情况发生。

我只是做错了吗?我尝试过不使用分隔符、分号、逗号……这似乎只是开发团队的疏忽。我感兴趣的是为什么这被省略了,如果我以错误的方式处理这个问题或者是否有人对替代方案有好的建议。

There seems to be no language syntax for specifying both a this() and a base() constructor. Given the following code:

public class Bar : Foo
{
    public Bar()
      :base(1)
      //:this(0)
    { }
    public Bar(int schmalue)
      :Foo(1)
    {
       //Gobs of initialization code
       ...
       Schmalue = schmalue;
    }

    public int Schmalue { get; private set; }
}

public class Foo
{
    public Foo()
    {
        Value = 0;
    }

    public class Foo(int value)
    {
        Value = value;
    }

    public int Value { get; private set; }
}

The compiler gives me an error stating that '{' was expected when uncommenting the :this(0) call. This is bothersome because it causes me to factor out my code into a private method, when this functionality was clearly provided to prevent such a thing.

Am I simply doing this wrong? I've attempted no separator, semi-colon, comma... It seems this was just an oversight from the dev team. I'm interested in why this was omitted, if I'm going about this the wrong way or if anybody has good suggestions for alternatives.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

煮酒 2024-10-22 10:23:31

您可以通过调用链末尾的基本构造函数来完成此操作:

public Bar(): this(0) {}
public Bar(int schmalue): base(1) {
    // ... initialize
}

You can accomplish this by invoking the base constructor at the end of your chain:

public Bar(): this(0) {}
public Bar(int schmalue): base(1) {
    // ... initialize
}
铁轨上的流浪者 2024-10-22 10:23:31

不,您只能链接到单个构造函数 - 同一类中的另一个构造函数或基构造函数。

目前还不清楚你想做什么。通常,我发现在派生类中创建一个“主”构造函数是值得的:派生类中的所有其他构造函数都使用“this”来调用主构造函数,主构造函数调用“base”来调用适当的基构造函数。

虽然该模型并不适合所有场景 - 特别是当您想要从不同的派生构造函数调用不同的基构造函数时 - 它通常是一个很好的模型。

No, you can only chain to a single constructor - either another constructor in the same class or a base constructor.

It's not really clear what you're trying to do. Typically I find it's worth creating one "primary" constructor in a derived class: all the other constructors within the derived class use "this" to call the primary one, which calls "base" to call the appropriate base constructor.

While that model doesn't fit every scenario - in particular when you want to call different base constructors from different derived constructors - it's usually a good model.

荆棘i 2024-10-22 10:23:31

考虑一下如果您可以在一个构造函数中同时调用 thisbase 作为构造函数,会发生什么情况。我们假设首先将调用基本构造函数。然后,将调用 this 构造函数 - 它本身将调用基本构造函数。因此,基类将被实例化两次。这打破了构造函数的语义——即对象被构造一次。

因此,禁止同时调用 basethis。如果需要,让您的委托 this 构造函数使用特定参数调用基类。

Consider what would happen if you could call both this and base as constructors in one constructor. Let's assume that first the base constructor would be called. Then, the this constructor would be called - which itself will call the base constructor. Thus, the base class would be instantiated twice. This breaks the semantics of constructors - namely, that an object is constructed once.

As such, calling both base and this is forbidden. Let your delegated this constructor call the base with specific parameters, if it needs to.

長街聽風 2024-10-22 10:23:31

在您的设计中,我看不到 BarFoo 类之间的许多共同点。当您重新实现所有内容时,为什么 Foo 派生自 Bar?这两个类都有一个带有公共 getter 和私有 setter 的整数属性,并且两个类都有默认构造函数和一个允许初始化整数属性的构造函数。那么为什么这两个类会存在呢?

In your design I can't see many common things between the Bar and Foo classes. Why does Foo derive from Bar when you reimplement everything? Both classes have an integer property with public getter and private setter and both classes have default constructors and a constructor allowing to initialize the integer property. So why do those two classes exist anyways?

眼眸里的那抹悲凉 2024-10-22 10:23:31

《巴尔》中的第二位演员完全错了。尝试:

public Bar(int schmalue)
  :base(1) //would call Foo(1)
{
   //Gobs of initialization code
   ...
   Schmalue = schmalue;
}

第一个ctor中的注释似乎意味着

  • initialize Bar with schmalue = 0

  • call base ctor Foo with value = 1

对吗?

为此,替换第二个 ctor 并简单地添加另一个可以处理这两个值的(私有)ctor

public Bar(int schmalue)
  :this(1, schmalue) //would call Bar(1, schmalue)
{
}

private Bar(int value, int schmalue)
 :base(value)
{
    //Gobs of initialization code
   ...
   Schmalue = schmalue;
}

The second ctor in Bar is simply wrong. Try:

public Bar(int schmalue)
  :base(1) //would call Foo(1)
{
   //Gobs of initialization code
   ...
   Schmalue = schmalue;
}

The comment in the first ctor seems to mean something like

  • initialize Bar with schmalue = 0

  • call base ctor Foo with value = 1

Right?

To do that, replace the second ctor and simply add another (private) ctor that can handle both values

public Bar(int schmalue)
  :this(1, schmalue) //would call Bar(1, schmalue)
{
}

private Bar(int value, int schmalue)
 :base(value)
{
    //Gobs of initialization code
   ...
   Schmalue = schmalue;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文