在构造函数中设置私有字段 [英] setting private fields in a constructor

查看:86
本文介绍了在构造函数中设置私有字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果一个类使用公共属性来公开私有字段,并且有一个

构造函数来初始化这些字段,那么构造函数应该直接设置它们

还是使用set访问器?或者重要吗?


Michael Roper

If a class uses public properties to expose private fields, and has a
constructor to initialize those fields, should the constructor set them
directly or use the set accessors? Or does it matter?

Michael Roper

推荐答案

我认为不是一般回答这个问题。


如果设置的访问者只是设置了字段而没有更多,那么就没有多少赔率。有人可能会担心set accessor有点慢,

但我认为在大多数情况下会优化掉。但是,即使

设置的访问器没有什么特别之处,总会出现这样的问题:

将来可能会做些什么。


如果设置的访问者确实做了一些特别的事情,那么它是一个问题

你是否想做那件特别的事情。作为课程设计师,你知道需要做什么,你可以做出决定。


例如,假设你有一个确定int的set访问器在0-10范围内是
,如果不是则抛出异常。如果你在构造函数中使用

硬编码0来调用它,那么你就是在浪费时间通过

set访问器。但是,如果限制要改变,那么0

可能会变得无效,你会想要抓住它。


如果设置的访问者是比方说,计算

字段被更改的次数,那么你肯定不想从构造函数中增加

计数,因为那是初始化,不要改变。


要考虑的另一个问题是该属性是否为虚拟属性。如果是这样,那么你

可能应该调用set访问器来获取任何派生的

功能,但这又可能取决于上下文。此外,从构造函数调用

虚拟方法总是可疑的 - 在C#

中比在C ++中更少,但仍然不是很好。

问候,


贾斯珀肯特


***通过Developersdex发送 http://www.developersdex.com ***

不要只是参加USENET ......获得奖励!
I don''t think there''s a general answer to this one.

If the set accessor simply sets the field and nothing more, then there''s
not much odds. One might worry that the set accessor is a bit slower,
but I think that will be optimised away in most cases. However, even if
the set accessor does nothing special, there is always the issue that it
might do something in future.

If the set accessor does do something special, then its a question of
whether you want to do that special thing. As class desginer you know
what needs to be done and you can make the decisions.

For example, suppose you have a set accessor that ensures an int is in
the range 0-10, and throws an exception if not. If you call it with a
hard-coded 0 in the constructor, then you''re wasting time going through
the set accessor. However, if the restriction were to change, then 0
might become invalid and you''d want to catch that.

If the set accessor were to, say, count the number of times that the
field was changed, then you certainly wouldn''t want to increment that
count from the constructor, because thats initialisation, not change.

Another issue to consider is if the property is virtual. If so, then you
probably should call the set accessor to pick up any derived
functionality, but again it may depend on the context. Also, calling
virtual methods from a constructor is always dubious - less so in C#
than it was in C++, but still not great.

Regards,

Jasper Kent

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!


感谢Jasper的周到回复。


Michael Roper
Thanks for the thoughtful reply Jasper.

Michael Roper


只需加入我的两美分。我几乎总是在我自己的类中使用属​​性getter /

setter - 包括构造函数。如果我正在做任何

类别的边界检查/错误检查,那么我当然要确保我继续在我班级内外使用
。纯粹的化妆品,但是我认为代码更容易阅读 - 至少对我而言:


public MyClass()

{

宽度= 10;

}


vs.


public MyClass()

{

_width = 10; //或

this.width = 10;

}


另外,我认为它更有助于封装。假设您将_width和

_height移动到_size或_rectangle结构中,然后您只需更改

属性,其余代码仍可正常工作。


当然,有些地方我没有使用该属性(对于

示例,也许我需要在我的班级中将一个字段重置为null,但是我不要
允许其他课程这样做)。但大多数时候,我更喜欢使用

财产。


-

Mike Mayer
< a rel =nofollowhref =http://www.mag37.com/csharp/target =_ blank> http://www.mag37.com/csharp/
mi ** @ mag37.com

" Jasper Kent" < JA ********* @ hotmail.com>在消息中写道

news:eT ************** @ TK2MSFTNGP10.phx.gbl ...
Just to put in my two-cents. I almost always use the property getter /
setter within my own classes - including the constructor. If I''m doing any
sort of bounds checking / error checking then I surely want to make sure I
continue to use that, inside or outside of my class. Purely cosmetic, but I
generally think the code is easier to read - at least for me:

public MyClass()
{
Width = 10;
}

vs.

public MyClass()
{
_width = 10; // or
this.width = 10;
}

Plus, I think it helps encapsulation even more. Say you move _width and
_height into a _size or _rectangle structure, then you just change your
properties and the rest of your code still works.

There are, of course, some places where I don''t use the property (for
example, maybe I need to reset a field to null within my class, but I don''t
allow other classes to do that). But most of the time, I prefer to use the
property.

--
Mike Mayer
http://www.mag37.com/csharp/
mi**@mag37.com
"Jasper Kent" <ja*********@hotmail.com> wrote in message
news:eT**************@TK2MSFTNGP10.phx.gbl...
我不是我觉得这个问题有一般的答案。

如果设置的访问者只是设置了字段,那么就没那么多了。有人可能会担心set accessor有点慢,但我认为在大多数情况下都会优化掉。但是,即使设置的访问器没有什么特别之处,总会存在以后它可能会做某事的问题。

如果set accessor确实做了一些特别的事情,那么这个问题是否你想做那件特别的事。作为课程设计师你知道需要做什么,你可以做出决定。

例如,假设你有一个设置访问器,确保int在
范围内0-10,如果没有则抛出异常。如果你在构造函数中使用
硬编码0来调用它,那么你就是在浪费时间浏览
set accessor。但是,如果限制要改变,那么0
可能会变得无效,你会想要抓住它。

如果设置的访问者是,比如计算次数那个
字段已被更改,那么你肯定不想从构造函数中增加那个
数,因为那是初始化,而不是改变。

要考虑的另一个问题是如果属性是虚拟的。如果是这样,那么你可能应该调用set访问器来获取任何派生的
功能,但同样可能取决于上下文。此外,从构造函数调用虚拟方法总是可疑 - 在C#中比在C ++中更少,但仍然不是很好。

问候,
< Jasper Kent

***通过开发人员指南 http:// www发送.developersdex.com ***
不要只是参加USENET ......获得奖励!
I don''t think there''s a general answer to this one.

If the set accessor simply sets the field and nothing more, then there''s
not much odds. One might worry that the set accessor is a bit slower,
but I think that will be optimised away in most cases. However, even if
the set accessor does nothing special, there is always the issue that it
might do something in future.

If the set accessor does do something special, then its a question of
whether you want to do that special thing. As class desginer you know
what needs to be done and you can make the decisions.

For example, suppose you have a set accessor that ensures an int is in
the range 0-10, and throws an exception if not. If you call it with a
hard-coded 0 in the constructor, then you''re wasting time going through
the set accessor. However, if the restriction were to change, then 0
might become invalid and you''d want to catch that.

If the set accessor were to, say, count the number of times that the
field was changed, then you certainly wouldn''t want to increment that
count from the constructor, because thats initialisation, not change.

Another issue to consider is if the property is virtual. If so, then you
probably should call the set accessor to pick up any derived
functionality, but again it may depend on the context. Also, calling
virtual methods from a constructor is always dubious - less so in C#
than it was in C++, but still not great.

Regards,

Jasper Kent

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!



这篇关于在构造函数中设置私有字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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