C# 中的 this() 和 base() 构造函数
似乎没有语言语法可以同时指定 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以通过调用链末尾的基本构造函数来完成此操作:
You can accomplish this by invoking the base constructor at the end of your chain:
不,您只能链接到单个构造函数 - 同一类中的另一个构造函数或基构造函数。
目前还不清楚你想做什么。通常,我发现在派生类中创建一个“主”构造函数是值得的:派生类中的所有其他构造函数都使用“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.
考虑一下如果您可以在一个构造函数中同时调用
this
和base
作为构造函数,会发生什么情况。我们假设首先将调用基本构造函数。然后,将调用this
构造函数 - 它本身将调用基本构造函数。因此,基类将被实例化两次。这打破了构造函数的语义——即对象被构造一次。因此,禁止同时调用
base
和this
。如果需要,让您的委托this
构造函数使用特定参数调用基类。Consider what would happen if you could call both
this
andbase
as constructors in one constructor. Let's assume that first the base constructor would be called. Then, thethis
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
andthis
is forbidden. Let your delegatedthis
constructor call the base with specific parameters, if it needs to.在您的设计中,我看不到
Bar
和Foo
类之间的许多共同点。当您重新实现所有内容时,为什么Foo
派生自Bar
?这两个类都有一个带有公共 getter 和私有 setter 的整数属性,并且两个类都有默认构造函数和一个允许初始化整数属性的构造函数。那么为什么这两个类会存在呢?In your design I can't see many common things between the
Bar
andFoo
classes. Why doesFoo
derive fromBar
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?《巴尔》中的第二位演员完全错了。尝试:
第一个ctor中的注释似乎意味着
initialize Bar with schmalue = 0
call base ctor Foo with value = 1
对吗?
为此,替换第二个 ctor 并简单地添加另一个可以处理这两个值的(私有)ctor
The second ctor in Bar is simply wrong. Try:
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