财产还是职能? [英] Property or Function?

查看:63
本文介绍了财产还是职能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现自己做了以前没有的事情:

< code>

public uint Duration

{

get

{

uint duration = 0;

foreach(m_things中的东西)

{

持续时间+ = t.Duration;

}


返回时间;

}

}

< / code>


这让我觉得......可以这样做吗?东西"在一个属性?和怎么做

你决定使用房产或功能?


所以,这是我的问题:是否有指导方针决定什么进入

属性和什么应该是一个函数?

I just found myself doing something I haven''t before:
<code>
public uint Duration
{
get
{
uint duration = 0;
foreach(Thing t in m_things)
{
duration += t.Duration;
}

return duration;
}
}
</code>

And it made me think... "Is that OK to do stuff" in a property?" and "How do
you decide to use a property or a function?"

So, that''s my question: Are there guidelines for deciding what goes into a
property and what should be a function?

推荐答案

你有任何特定的信息使得你认为你不能或不能在物业中做这种事情吗?


在我看来,房产可以是被认为是一个形容词(一个描述单词的b $ b),并且一种方法可以被认为是一种动词(一个

动作词) )。


以动物类为例,它可能有一个布尔属性

''EatsGrass''和方法''EatGrass' 。前者描述'是否'

动物确实吃草,而后者则指示动物执行动物''b $ b''动作''吃草。 br />

有时将是否实现某种物品作为一种物品或方法的区别可能是微妙的,有时它可能只是一个问题

选择。


在你的情况下,很明显你有一个物品集合(物品)

物品。虽然每个Thing对象都有一个Duration属性,但是物品

集合的持续时间属性是持续时间属性的总和

的成员。


在这种情况下,属性的使用是有效的,因为持续时间描述了关于Thing对象集合的内容。

" sklett" < SK **** @ mddirect.com>在消息中写道

新闻:OL ************* @ TK2MSFTNGP05.phx.gbl ...
Do you have any specific information that makes you think that you can''t or
shouldn''t do this sort of thing in a property?

In my view, a property can be considered to be a''kin to an adjective (a
describing word) and a method can be considered to be a''kin to a verb (an
action word).

Taking, as an example, an animal class, it might have a boolean property
''EatsGrass'' and a method ''EatGrass''. The former ''describes'' whether or not
the animal does eat grass while the latter instructs the animal to carry out
the ''action'' eat grass.

Sometimes the distinction between whether to implement something as a
property or a method can be subtle and sometimes it can simply be a matter
of choice.

In your case, it is clear that you have a collection (Things) of Thing
objects. While each Thing object has a Duration property, the Things
collection has a Duration property being the sum of the Duration properties
of it''s members.

In this case the use of a property is valid because Duration describes
something about the collection of Thing objects.
"sklett" <sk****@mddirect.com> wrote in message
news:OL*************@TK2MSFTNGP05.phx.gbl...
我发现自己在做某事我以前没有:
< code>
public uint持续时间
{
获得
{
uint duration = 0;
foreach(m_things中的东西)
{
持续时间+ = t.Duration;
}

返回持续时间;
}
}
< / code>

这让我觉得......做的事情还可以吗在一个属性?和如何
你决定使用一个属性或功能?

所以,这是我的问题:是否有指导决定什么进入
一个属性和什么应该是一个函数?
I just found myself doing something I haven''t before:
<code>
public uint Duration
{
get
{
uint duration = 0;
foreach(Thing t in m_things)
{
duration += t.Duration;
}

return duration;
}
}
</code>

And it made me think... "Is that OK to do stuff" in a property?" and "How
do you decide to use a property or a function?"

So, that''s my question: Are there guidelines for deciding what goes into
a property and what should be a function?



在这个特定的例子中,我会在一个属性上选择一个函数。

原因是因为很多(大多数?)人认为属性是

恒定时间操作。他们可能会重复致电该物业,而不会实现其可能产生的性能效果。


如果您仍然认为这样做会使其成为一个财产,那么至少

文件说运行时是线性的而不是常数。


另外,我相信微软的框架设计指南书

。 .NET开发系列建议保持属性的运行时间

不变。


Brian


sklett写道:
In this particular example I would chose a function over a property.
The reason is because a lot (most?) people assume that properties are
constant time operations. They may repeated call the property without
realizing the performance effect it may have.

If you still think it makes since to make it a property then at least
document that runtime is linear and not constant.

Also, I believe the Framework Design Guidelines book in the Microsoft
..NET Development Series recommends keeping the runtime of properties
constant.

Brian

sklett wrote:
我发现自己做了一些我以前没做过的事情:
< code>
public uint Duration
{
get
{
uint duration = 0;
foreach(m_things中的东西)
{
持续时间+ = t.Duration;
}

返回持续时间;
}
}
< / code>

它让我想到了......可以做些什么吗?在一个属性?和你如何决定使用财产或功能?

所以,这是我的问题:是否有指导决定什么进入属性和应该是什么功能?
I just found myself doing something I haven''t before:
<code>
public uint Duration
{
get
{
uint duration = 0;
foreach(Thing t in m_things)
{
duration += t.Duration;
}

return duration;
}
}
</code>

And it made me think... "Is that OK to do stuff" in a property?" and "How do
you decide to use a property or a function?"

So, that''s my question: Are there guidelines for deciding what goes into a
property and what should be a function?








" sklett" < SK **** @ mddirect.com>在消息中写道

新闻:OL ************* @ TK2MSFTNGP05.phx.gbl ...

"sklett" <sk****@mddirect.com> wrote in message
news:OL*************@TK2MSFTNGP05.phx.gbl...
我发现自己在做某事我以前没有:
< code>
public uint持续时间
{
获得
{
uint duration = 0;
foreach(m_things中的东西)
{
持续时间+ = t.Duration;
}

返回持续时间;
}
}
< / code>

这让我觉得......做的事情还可以吗在一个属性?和如何
你决定使用一个属性或功能?

所以,这是我的问题:是否有指导决定什么进入
一个属性和什么应该是一个功能?


我不知道任何guidlines,但信息隐藏可以是一双双刃剑。在我看来,得到没有设置参考

类型非常好,但在用于返回值时会混淆。


David
I just found myself doing something I haven''t before:
<code>
public uint Duration
{
get
{
uint duration = 0;
foreach(Thing t in m_things)
{
duration += t.Duration;
}

return duration;
}
}
</code>

And it made me think... "Is that OK to do stuff" in a property?" and "How
do you decide to use a property or a function?"

So, that''s my question: Are there guidelines for deciding what goes into
a property and what should be a function?
I don''t know about any guidlines but "information hiding" can be a double
edged sword. In my view "get" without "set" works very well with reference
types but confuses things when used to return values.

David



这篇关于财产还是职能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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