如何在 C# 中进行构造函数链接
我知道这应该是一个超级简单的问题,但我已经为这个概念苦苦挣扎了一段时间。
我的问题是,如何在 C# 中链接构造函数?
我正在参加我的第一堂 OOP 课程,所以我只是在学习。我不明白构造函数链接是如何工作的,也不明白如何实现它,甚至不明白为什么它比只在没有链接的情况下创建构造函数更好。
我希望有一些带有解释的例子。
那么如何链接它们呢?
我知道两个是这样的:
public SomeClass this: {0}
public SomeClass
{
someVariable = 0
}
但是如果是三个、四个等等,你怎么做呢?
再说一遍,我知道这是一个初学者问题,但我很难理解这一点,而且我不知道为什么。
I know that this is supposedly a super simple question, but I've been struggling with the concept for some time now.
My question is, how do you chain constructors in C#?
I'm in my first OOP class, so I'm just learning. I don't understand how constructor chaining works or how to implement it or even why it's better than just doing constructors without chaining.
I would appreciate some examples with an explanation.
So how do how chain them?
I know with two it goes:
public SomeClass this: {0}
public SomeClass
{
someVariable = 0
}
But how do you do it with three, four and so on?
Again, I know this is a beginner question, but I'm struggling to understand this and I don't know why.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您可以使用标准语法(像方法一样使用
this
)来选择类内部的重载:then:
另请注意:
base(...)
base()
对于“为什么?”:
有必要调用非默认基构造函数,例如:
请注意,您也可以以类似的方式使用对象初始值设定项(无需编写任何内容) :
You use standard syntax (using
this
like a method) to pick the overload, inside the class:then:
Note also:
base(...)
base()
For "why?":
necessary to call a non-default base-constructor, for example:
Note that you can also use object initializers in a similar way, though (without needing to write anything):
我只是想向任何搜索此内容的人提出一个有效的观点。如果您要使用 4.0 (VS2010) 之前的 .NET 版本,请注意您必须创建如上所示的构造函数链。
但是,如果您仍使用 4.0,我有个好消息。您现在可以拥有一个带有可选参数的构造函数!我将简化 Foo 类示例:
当您实现构造函数时,您可以使用可选参数,因为默认值已设置。
我希望你喜欢这节课!我简直不敢相信,自 2004/2005 年以来,开发人员一直在抱怨构造链并且无法使用默认的可选参数!现在它在开发领域已经花了很长时间,开发人员害怕使用它,因为它不向后兼容。
I just want to bring up a valid point to anyone searching for this. If you are going to work with .NET versions before 4.0 (VS2010), please be advised that you have to create constructor chains as shown above.
However, if you're staying in 4.0, I have good news. You can now have a single constructor with optional arguments! I'll simplify the Foo class example:
When you implement the constructor, you can use the optional arguments since defaults have been set.
I hope you enjoyed this lesson! I just can't believe that developers have been complaining about construct chaining and not being able to use default optional arguments since 2004/2005! Now it has taken SO long in the development world, that developers are afraid of using it because it won't be backwards compatible.
最好用一个例子来说明这一点。想象一下,我们有一个 Person 类,
所以这里我们有一个构造函数,它设置一些属性,并使用构造函数链来允许您仅使用名称或仅使用名称和地址来创建对象。如果您创建一个仅包含名称的实例,则会将默认值 string.Empty 发送到名称和地址,然后将邮政编码的默认值发送到最终的构造函数。
这样做可以减少您编写的代码量。实际上只有一个构造函数包含代码,您不必重复自己,因此,例如,如果您将 Name 从属性更改为内部字段,则只需更改一个构造函数 - 如果您在所有三个构造函数中设置该属性那将是三个地方可以改变它。
This is best illustrated with an example. Imaging we have a class Person
So here we have a constructor which sets some properties, and uses constructor chaining to allow you to create the object with just a name, or just a name and address. If you create an instance with just a name this will send a default value, string.Empty through to the name and address, which then sends a default value for Postcode through to the final constructor.
In doing so you're reducing the amount of code you've written. Only one constructor actually has code in it, you're not repeating yourself, so, for example, if you change Name from a property to an internal field you need only change one constructor - if you'd set that property in all three constructors that would be three places to change it.
“构造函数链”的用法是什么?
您可以使用它从一个构造函数调用另一个构造函数。
如何实现“构造器链”?
在构造函数定义后使用“:this(yourProperties)”关键字。例如:
为什么有用?
重要原因是减少编码,防止重复代码。例如初始化属性的重复代码
假设类中的某些属性必须使用特定值进行初始化(在我们的示例中为 requestDate)。并且类有2个或更多构造函数。如果没有“构造函数链”,则必须在类的所有构造函数中重复初始化代码。
它是如何工作的? (或者,“构造函数链”中的执行顺序是什么)?
在上面的例子中,方法“a”将首先被执行,然后指令序列将返回到方法“b”。
换句话说,上面的代码与下面的代码相同:
What is usage of "Constructor Chain"?
You use it for calling one constructor from another constructor.
How can implement "Constructor Chain"?
Use ": this (yourProperties)" keyword after definition of constructor. for example:
Why is it useful?
Important reason is reduce coding, and prevention of duplicate code. such as repeated code for initializing property
Suppose some property in class must be initialized with specific value (In our sample, requestDate). And class have 2 or more constructor. Without "Constructor Chain", you must repeat initializaion code in all constractors of class.
How it work? (Or, What is execution sequence in "Constructor Chain")?
in above example, method "a" will be executed first, and then instruction sequence will return to method "b".
In other word, above code is equal with below:
我有一个日记班,所以我不会一次又一次地写设置值
I have a diary class and so i am not writing setting the values again and again
所有这些答案都很好,但我想在具有更复杂初始化的构造函数上添加注释。
尽管这个例子也可以在没有这个静态函数的情况下解决,但静态函数允许更复杂的逻辑,甚至可以从其他地方调用方法。
All those answers are good, but I'd like to add a note on constructors with a little more complex initializations.
Allthogh this example might as well be solved without this static function, the static function allows for more complex logic, or even calling methods from somewhere else.
你是问这个吗?
Are you asking about this?
我希望下面的示例能够对构造函数链有所启发。
例如,我的用例是,您希望用户将目录传递给您的
构造函数,用户不知道要传递哪个目录并决定让
您指定默认目录。您指定一个您认为的默认目录
会起作用的。
顺便说一句,我在这个示例中使用了 LINQPad,以防您想知道 *.Dump() 是什么。
干杯
I hope following example shed some light on constructor chaining.
my use case here for example, you are expecting user to pass a directory to your
constructor, user doesn't know what directory to pass and decides to let
you assign default directory. you step up and assign a default directory that you think
will work.
BTW, I used LINQPad for this example in case you are wondering what *.Dump() is.
cheers
构造函数链接中还有一个重要的点:顺序。
为什么?
假设您有一个在运行时由框架构造的对象,该框架期望它是默认构造函数。如果您希望能够传递值,同时仍然能够在需要时传递构造函数参数,那么这是非常有用的。
例如,我可以有一个支持变量,它被我的默认构造函数设置为默认值,但能够被覆盖。
There's another important point in constructor chaining: order.
Why?
Let's say that you have an object being constructed at runtime by a framework that expects it's default constructor. If you want to be able to pass in values while still having the ability to pass in constructor argments when you want, this is extremely useful.
I could for instance have a backing variable that gets set to a default value by my default constructor but has the ability to be overwritten.