为什么简单属性? [英] Why simple properties?

查看:56
本文介绍了为什么简单属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果你想限制访问,我可以理解为什么物业很整洁

(只有获取,没有设定),或者你想做一些簿记或理智

检查on values(in set)或者如果你想更改底层的

类型而不改变属性类型等。


但是对于像这样的属性有什么用? :


私有int数据;


public int数据

{

get

{

返回数据;

}


设定

{

data = value;

}

}




我看到了很多。出于所有意图和目的,您可能只需要一个

公共成员。即使您想稍后将公共成员

更改为属性,您的界面仍将保留。没有

可能性丢失。


/ David

I can understand why properties are neat if you want to limit access
(only get, no set), or you want to do some bookkeeping or sanity
checking on values (in set) or if you want to change the underlying
type without changing the property type etc.

But what is the use for properties like:

private int data;

public int Data
{
get
{
return data;
}

set
{
data = value;
}
}
?

I see this a lot. For all intents and purposes, you might just have a
public member instead. Even if you wanted to change the public member
later to a property, your interface would still be maintained. No
possibilities lost.

/David

推荐答案

10月3日上午10点28分,pinkfloydho ... @ gmail.com

< pinkfloydho ... @ gmail.comwrote:


< snip>
On Oct 3, 10:28 am, "pinkfloydho...@gmail.com"
<pinkfloydho...@gmail.comwrote:

<snip>

我看到了很多。出于所有意图和目的,您可能只需要一个

公共成员。即使您想稍后将公共成员

更改为属性,您的界面仍将保留。没有

可能性丢失。
I see this a lot. For all intents and purposes, you might just have a
public member instead. Even if you wanted to change the public member
later to a property, your interface would still be maintained. No
possibilities lost.



不,界面不被维护。有各种各样的东西,你可以用字段(例如通过引用传递它们)来做你不能做的事情

带有属性。同样地,有些东西与属性一起工作

(例如数据绑定),它们不适用于字段。使用

反射或序列化的任何东西都可能会破坏。


从一开始就最好使用属性 - 我不是喜欢

任何不私密的领域。我看到整个属性代码都有点痛苦,但是C#3会让这个变得更简单:


public string Foo {get;私人集;例如,



Jon

No, the interface isn''t maintained. There are various things you can
do with fields (such as passing them by reference) which you can''t do
with properties. Likewise there are things which work with properties
(such as databinding) which don''t work with fields. Anything using
reflection or serialization is likely to break as well.

It''s best to use properties right from the start - I don''t like having
any fields which aren''t private. I see that it''s a slight pain to have
the whole property code, but C# 3 will make this simpler:

public string Foo { get; private set; }

for instance.

Jon


On Oct 3,5:28 am,pinkfloydho ... @ gmail.com

< pinkfloydho ... @ gmail.comwrote:
On Oct 3, 5:28 am, "pinkfloydho...@gmail.com"
<pinkfloydho...@gmail.comwrote:

如果你想限制访问,我可以理解为什么房产很整洁

(只有获取,没有设定),或者你想做一些记账或理智

检查on values(in set)或者如果你想更改底层的

类型而不改变属性类型等。


但是对于像这样的属性有什么用? :


私有int数据;


public int数据

{

get

{

返回数据;

}


设定

{

data = value;

}


}





我看到了很多。出于所有意图和目的,您可能只需要一个

公共成员。即使您想稍后将公共成员

更改为属性,您的界面仍将保留。没有

可能性丢失。


/ David
I can understand why properties are neat if you want to limit access
(only get, no set), or you want to do some bookkeeping or sanity
checking on values (in set) or if you want to change the underlying
type without changing the property type etc.

But what is the use for properties like:

private int data;

public int Data
{
get
{
return data;
}

set
{
data = value;
}

}

?

I see this a lot. For all intents and purposes, you might just have a
public member instead. Even if you wanted to change the public member
later to a property, your interface would still be maintained. No
possibilities lost.

/David



大卫,

除了Jon所说的,我还发现将

属性访问器作为设置断点的中心位置(或添加日志

行)很方便帮助调试。

John

Hi David,
In addition to what Jon said, I also find it handy to have the
property accessors as a central place to set a breakpoint (or add log
line) to help in debugging.
John


作为其他人所说的补充,请考虑一下你的

目前的需求也可能不是您未来的需求。你真的要去

想要查看所有内容并将你的变量用法改为财产

如果你确定你需要某种理智检查/其他东西吗?每当分配该属性时,
会继续吗?就我个人而言,我认为它还可以帮助我们预防未来。


克里斯。
As an addition to what other people have said, consider that your
current needs may also not be your future needs. Are you really going to
want to go through everything and change your variable use to property
use if you decide you do need some kind of sanity checking/other things
going on whenever that property is assigned? Personally, I think it
helps a bit with future-proofing as well.

Chris.


这篇关于为什么简单属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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