如何在 C# 中进行构造函数链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1814953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to do constructor chaining in C#
提问by Alex
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#?
我的问题是,你如何在 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.
我在上我的第一个 OOP 课程,所以我只是在学习。我不明白构造函数链接如何工作或如何实现它,甚至不明白为什么它比只做没有链接的构造函数更好。
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.
同样,我知道这是一个初学者问题,但我很难理解这一点,我不知道为什么。
采纳答案by Marc Gravell
You use standard syntax (using this
like a method) to pick the overload, insidethe class:
您可以使用标准语法(使用this
类似的方法)来挑过载,内部类:
class Foo
{
private int id;
private string name;
public Foo() : this(0, "")
{
}
public Foo(int id, string name)
{
this.id = id;
this.name = name;
}
public Foo(int id) : this(id, "")
{
}
public Foo(string name) : this(0, name)
{
}
}
then:
然后:
Foo a = new Foo(), b = new Foo(456,"def"), c = new Foo(123), d = new Foo("abc");
Note also:
另请注意:
- you can chain to constructors on the base-type using
base(...)
- you can put extra code into each constructor
- the default (if you don't specify anything) is
base()
- 您可以使用链接到基类型上的构造函数
base(...)
- 您可以将额外的代码放入每个构造函数中
- 默认值(如果您未指定任何内容)是
base()
For "why?":
对于“为什么?”:
- code reduction (always a good thing)
necessaryto call a non-default base-constructor, for example:
SomeBaseType(int id) : base(id) {...}
- 代码减少(总是一件好事)
调用非默认基构造函数所必需的,例如:
SomeBaseType(int id) : base(id) {...}
Note that you can also use object initializers in a similar way, though (without needing to write anything):
请注意,您也可以以类似的方式使用对象初始值设定项(无需编写任何内容):
SomeType x = new SomeType(), y = new SomeType { Key = "abc" },
z = new SomeType { DoB = DateTime.Today };
回答by Manish Basantani
Are you asking about this?
你问这个?
public class VariantDate {
public int day;
public int month;
public int year;
public VariantDate(int day) : this(day, 1) {}
public VariantDate(int day, int month) : this(day, month,1900){}
public VariantDate(int day, int month, int year){
this.day=day;
this.month=month;
this.year=year;
}
}
回答by blowdart
This is best illustrated with an example. Imaging we have a class Person
这最好用一个例子来说明。成像我们有一个类 Person
public Person(string name) : this(name, string.Empty)
{
}
public Person(string name, string address) : this(name, address, string.Empty)
{
}
public Person(string name, string address, string postcode)
{
this.Name = name;
this.Address = address;
this.Postcode = postcode;
}
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.
所以这里我们有一个构造函数,它设置一些属性,并使用构造函数链来允许你创建只有一个名字的对象,或者只有一个名字和地址。如果您创建一个只有名称的实例,这将向名称和地址发送一个默认值 string.Empty,然后将 Postcode 的默认值发送到最终构造函数。
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.
这样做可以减少您编写的代码量。只有一个构造函数实际上包含代码,您不会重复自己,因此,例如,如果将 Name 从属性更改为内部字段,则只需更改一个构造函数 - 如果您在所有三个构造函数中都设置了该属性这将是改变它的三个地方。
回答by ES3178
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.
我只想向任何搜索此内容的人提出一个有效的观点。如果您打算使用 4.0 (VS2010) 之前的 .NET 版本,请注意您必须创建如上所示的构造函数链。
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:
但是,如果您继续使用 4.0,我有个好消息。您现在可以拥有一个带有可选参数的构造函数!我将简化 Foo 类示例:
class Foo {
private int id;
private string name;
public Foo(int id = 0, string name = "") {
this.id = id;
this.name = name;
}
}
class Main() {
// Foo Int:
Foo myFooOne = new Foo(12);
// Foo String:
Foo myFooTwo = new Foo(name:"Timothy");
// Foo Both:
Foo myFooThree = new Foo(13, name:"Monkey");
}
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.
我希望你喜欢这节课!我简直不敢相信,自 2004/2005 年以来,开发人员一直在抱怨构造链并且无法使用默认的可选参数!现在在开发世界中已经花费了很长时间,以至于开发人员害怕使用它,因为它不会向后兼容。
回答by Jhankar Mahbub
I have a diary class and so i am not writing setting the values again and again
我有一个日记课,所以我不会一次又一次地写设置值
public Diary() {
this.Like = defaultLike;
this.Dislike = defaultDislike;
}
public Diary(string title, string diary): this()
{
this.Title = title;
this.DiaryText = diary;
}
public Diary(string title, string diary, string category): this(title, diary) {
this.Category = category;
}
public Diary(int id, string title, string diary, string category)
: this(title, diary, category)
{
this.DiaryID = id;
}
回答by yantaq
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
我希望下面的例子对构造函数链有所了解。
例如,我的用例在这里,您希望用户将目录传递给您的构造函数,用户不知道要传递的目录并决定让您分配默认目录。你加强并分配一个你认为可以工作的默认目录。
顺便说一句,我在这个例子中使用了 LINQPad,以防你想知道 *.Dump() 是什么。
干杯
void Main()
{
CtorChaining ctorNoparam = new CtorChaining();
ctorNoparam.Dump();
//Result --> BaseDir C:\Program Files (x86)\Default\
CtorChaining ctorOneparam = new CtorChaining("c:\customDir");
ctorOneparam.Dump();
//Result --> BaseDir c:\customDir
}
public class CtorChaining
{
public string BaseDir;
public static string DefaultDir = @"C:\Program Files (x86)\Default\";
public CtorChaining(): this(null) {}
public CtorChaining(string baseDir): this(baseDir, DefaultDir){}
public CtorChaining(string baseDir, string defaultDir)
{
//if baseDir == null, this.BaseDir = @"C:\Program Files (x86)\Default\"
this.BaseDir = baseDir ?? defaultDir;
}
}
回答by arviman
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.
例如,我可以有一个支持变量,该变量由我的默认构造函数设置为默认值,但可以被覆盖。
public class MyClass
{
private IDependency _myDependency;
MyClass(){ _myDependency = new DefaultDependency(); }
MYClass(IMyDependency dependency) : this() {
_myDependency = dependency; //now our dependency object replaces the defaultDependency
}
}
回答by M.i.T
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:
如何实现“构造函数链”?
在构造函数定义后使用“: this (yourProperties)”关键字。例如:
Class MyBillClass
{
private DateTime requestDate;
private int requestCount;
public MyBillClass()
{
/// ===== we naming "a" constructor ===== ///
requestDate = DateTime.Now;
}
public MyBillClass(int inputCount) : this()
{
/// ===== we naming "b" constructor ===== ///
/// ===== This method is "Chained Method" ===== ///
this.requestCount= inputCount;
}
}
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.
为什么有用?
重要的原因是减少编码,防止重复代码。例如初始化属性的重复代码假设类中的某些属性必须使用特定值(在我们的示例中为requestDate)进行初始化。并且类有 2 个或更多构造函数。如果没有“构造函数链”,则必须在类的所有构造函数中重复初始化代码。
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:
它是如何工作的?(或者,“构造函数链”中的执行顺序是什么)?
在上面的例子中,方法“a”将首先被执行,然后指令序列将返回到方法“b”。换句话说,上面的代码等同于下面的代码:
Class MyBillClass
{
private DateTime requestDate;
private int requestCount;
public MyBillClass()
{
/// ===== we naming "a" constructor ===== ///
requestDate = DateTime.Now;
}
public MyBillClass(int inputCount) : this()
{
/// ===== we naming "b" constructor ===== ///
// ===== This method is "Chained Method" ===== ///
/// *** --- > Compiler execute "MyBillClass()" first, And then continue instruction sequence from here
this.requestCount= inputCount;
}
}