重载的构造函数与对象初始化 [英] Overloaded constructors vs. Object initializations

查看:74
本文介绍了重载的构造函数与对象初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我想对重载构造函数与对象

初始化有所了解。假设该类支持默认构造函数,那么当你能够使用

对象初始化时,有没有理由包含重载的构造函数?我明白了一点,提供重载,如果你是开发框架代码,可能需要运行onx 2.x

,但如果你正在写一个密封的应用程序/知道你只需要在3.x或更高版本上进行
部署,然后对我的需求似乎并不明显。

..Andreas

Hi,

I would like to get some thoughts on Overloaded constructors vs. Object
initializations. Assuming that the class supports a default constructor, is
there any reason to include overloaded constructors when you are able to use
object initialization? I see a point, to provide the overloads, if you are
developing framework code that might support the need to run ontop of 2.x
but if you are writing a sealed application / know you are only going to
deploy on 3.x or higher, then the need doesn''t seem as apparent to me.
..Andreas

推荐答案

8月14日,3:48 * pm,Andreas< Andr ... @ discussion.microsoft.comwrote:
On Aug 14, 3:48*pm, Andreas <Andr...@discussions.microsoft.comwrote:




我想对重载构造函数与对象

初始化有所了解。假设该类支持默认构造函数,那么当你能够使用

对象初始化时,有没有理由包含重载的构造函数?
Hi,

I would like to get some thoughts on Overloaded constructors vs. Object
initializations. Assuming that the class supports a default constructor, is
there any reason to include overloaded constructors when you are able to use
object initialization?



是的,当你的对象是不可变的时候 - 那么你的所有属性都只是
readonly,并且不能使用对象初始值设定项。即使对于

可变对象,也可能只有在创建对象时初始化一次的属性,并且不会更改

之后 - 例如字典<,> .Comparer

Yes, when your object is immutable - then all your properties will be
readonly, and an object initializer cannot be used. And even for
mutable objects, there can be properties which should only be
initialized once when the object is created, and not changed
afterwards - e.g. Dictionary<,>.Comparer


我想了解一下重载构造函数与对象
I would like to get some thoughts on Overloaded constructors vs. Object

初始化。假设该类支持默认构造函数,那么当你能够使用

对象初始化时,有没有理由包含重载的构造函数?
initializations. Assuming that the class supports a default constructor, is
there any reason to include overloaded constructors when you are able to use
object initialization?



是的,当你的对象是不可变的时候 - 那么你的所有属性都将只读取b
,并且不能使用对象初始值设定项。即使对于

可变对象,也可能只有在创建对象时初始化一次的属性,并且不会更改

之后 - 例如字典<,> .Comparer


Yes, when your object is immutable - then all your properties will be
readonly, and an object initializer cannot be used. And even for
mutable objects, there can be properties which should only be
initialized once when the object is created, and not changed
afterwards - e.g. Dictionary<,>.Comparer



是的,你是对的。也许我应该更加精确地说

除了你明确要求使用非默认值

构造函数时。很多时候重载的构造函数已被用来使对象初始化更加用户友好并提供更清晰的代码,




公共类人员

{

公共人员()

{}


公共人员( string firstName,string lastName):this(0,firstName,

lastName)

{}


public Person(int age ,string firstName,string lastName)

{

......

}

}

Yes, you are right. Maybe I should have been a bit more precise by saying
"other than when you design explicitly require the use of non-default
constructor". Many times overloaded constructors have been used to make
object initialization a bit more userfriendly and to provide cleaner code,
such as

public class Person
{
public Person()
{ }

public Person(string firstName, string lastName) : this(0, firstName,
lastName)
{ }

public Person(int age, string firstName, string lastName)
{
......
}
}


8月14日,5:03 * pm,Andreas< Andr ... @ discussion.microsoft.comwrote:
On Aug 14, 5:03*pm, Andreas <Andr...@discussions.microsoft.comwrote:

是的,你是对的。也许我应该更加精确地说

除了你明确要求使用非默认值

构造函数时。很多时候重载的构造函数已被用来使对象初始化更加用户友好并提供更清晰的代码
Yes, you are right. Maybe I should have been a bit more precise by saying
"other than when you design explicitly require the use of non-default
constructor". Many times overloaded constructors have been used to make
object initialization a bit more userfriendly and to provide cleaner code



在该上下文中,对象初始化器显然是优越的(更多

可读,因为属性名称是拼写出来的)。除此之外,

a大量的房产,无论如何都不能涵盖所有的排列

(我经常在BCL看到这种半心半意的尝试,你在哪里比如,
需要一个属性#1 +#3 +#4的组合,但你只能得到构造函数

代表#1 +#2 +#3或#1 +# 2 +#4)。

In that context, object initializers are clearly superior (more
readable as well, since property names are spelled out). Besides, with
a significant number of properties, you can''t cover all permutations
anyway (I often see such half-hearted attempts in the BCL, where you
need, say, a combo of property #1+#3+#4, but you only get constructors
for #1+#2+#3 or #1+#2+#4).


这篇关于重载的构造函数与对象初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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