我怎么都称之为构造函数初始化,基地()和()这个? [英] how do i call both constructor initializers, base() and this()?

查看:168
本文介绍了我怎么都称之为构造函数初始化,基地()和()这个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是容易解决,但我只是好奇,如果我可以用一种语言特性或者可能的语言不允许它意味着我在做一流的设计逻辑错误的事实。

This is easy to work around, but I just curious if I could be using a language feature or possibly the fact that the language disallows it means I'm making a logical error in class design.

我在做我的代码自我审查,以帮助硬化它再利用和我刚来翻过:

I'm doing a self review of my code to help "harden" it for re-use and I just came accross:

public partial class TrackTyped : Component
{
    IContainer components = null;

    public TrackTyped()
        : base()
    {
        InitializeComponent();
    }

    public TrackTyped(IContainer container)
        : base()
    {
        container.Add(this);
        InitializeComponent();
    }
}

当我看到的同一行我最常做的在两个构造函数的代码是打一个电话,对方以这(),但我似乎无法做到这一点。

What I usually do when I see the same line of code in two constructors is make one call the other with "this()" but I can't seem to do it.

如果我读了规范正确(我只是开始尝试读取规格,所以我可能不适合):

If I read the spec right (I just started trying to read the spec so I may not be right):

10.11 Instance Constructors
   constructor-declarator:
      identifier   (   formal-parameter-listopt   )   constructor-initializeropt
   constructor-initializer:
      :   base   (   argument-listopt   )
      :   this   (   argument-listopt   )

它说我只能有其中的一个

It's saying I can only have one of those.

问:在10.11这意味着没有理由必须同时还是纯粹暗示的语言只支持调用一个?

QUESTION: is 10.11 implying that there's no reason to need to call both or is it simply implying that the language only supports calling one?

推荐答案

有没有需要通话双方,因为这个重定向到另一个构造函数,将调用

There is no need to call both, because this redirects to another constructor that will call base.

这篇关于我怎么都称之为构造函数初始化,基地()和()这个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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