为什么get / set属性有用? [英] Why are get/set properties useful?

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

问题描述

为什么get / set属性如此有用?我知道他们已被使用,所以我不会直接访问变量,但为什么这么有用呢?

与使用get / set之间的区别是什么?


谢谢。

WHy are the get/set properties so useful? I know they''re used so that I don''t
directly access a variable, but why is that so useful? What would the
difference be between using it directly and using get/set?

Thanks.

推荐答案

通过使用属性,而不是让他们访问的变量是公共的,你可以控制进入它们的数据,以及数据的方式

返回。例如,在set属性上,您可以验证数据,并且如果它无效,则返回错误。因此,如果您在

地址对象上有一个State属性,则可以确保它是51个有效条目之一。或者你

可以确保字符串适合数据库字段,然后才允许它插入字段中,因此你总是知道该对象是有效的。 br />

我没有找到get属性的多少用途,但是你可以将它用于

同样的东西。你也可以使用JUST一个get属性来制作一个

只读属性(所以一旦对象加载了来自

构造函数或fill函数的数据,外部应用程序和对象只能读取b



无论如何,你去了。我的
By using the properties, as opposed to making the variables they access
public, you can control what data goes into them, and how the data is
returned. For example, on the set property, you can validate the data and
return an error if it isn''t valid. So if you have a State property on an
address object, you can make sure that it''s one of 51 valid entries. Or you
can make sure a string will fit into a database field before you allow it to
be stuck into the field, so you always know the object is valid.

I haven''t found as much use for the get property, but you can use it for the
same sort of things. You can also use JUST a get property to make a
read-only property (so once the object is loaded with data from the
contructor or a "fill" function, external applications and objects can only
read the values).

Anyway, there you go. My


0.02。


Clint


" VMI" < VM*@discussions.microsoft.com>在留言中写道

新闻:4C ********************************** @ microsof t.com ...
0.02.

Clint

"VMI" <VM*@discussions.microsoft.com> wrote in message
news:4C**********************************@microsof t.com...
为什么get / set属性如此有用?我知道他们已被使用,所以我不会直接访问变量,但为什么这么有用呢?直接使用它和使用get / set之间的区别是什么?

谢谢。
WHy are the get/set properties so useful? I know they''re used so that I
don''t
directly access a variable, but why is that so useful? What would the
difference be between using it directly and using get/set?

Thanks.





VMI写道:

VMI wrote:
为什么get / set属性如此有用?我知道他们已被使用,所以我不会直接访问变量,但为什么这么有用呢?直接使用它和使用get / set之间的区别是什么?

谢谢。
WHy are the get/set properties so useful? I know they''re used so that I don''t
directly access a variable, but why is that so useful? What would the
difference be between using it directly and using get/set?

Thanks.




很多原因,但是最大的一个是控制和转换。


控制:

让我们假设由于某种原因,我的价值只能是1-10。

如果我允许用​​户指定超出该范围的值,我的程序

可能会b / b
崩溃或做一些奇怪的事情。如果setter不允许值为



范围1-10,我没有这个问题。同样地,让我们说我想要

来支付
当你设置其中一个值时更改一些OTHER变量。我可以在setter中做

。最后,假设我不想做的事情如果

你给我的

值与当前值相同。我可以在

setter中做到这一点。


转换:

如果我有一个设定例程,它取另一个值类型,比如一个

字符串

而不是一个整数,我可以把它转换成我想要的,而不用

用户

知道我的内部代表是什么。


Matt



Lots of reasons, but the biggest one is control and conversion.

Control:
Let''s assume that for some reason, my value can only be from 1-10.
If I allow the user to assign a value outside that range, my program
may
crash or do weird things. If the setter does not allow a value outside
the
range 1-10, I don''t have this problem. Likewise, let''s say that I want
to
change some OTHER variable when you set one of the values. I can do
that in the setter. Finally, suppose that I don''t want to do things if
the
value you give me is the same as the current value. I can do that in a
setter.

Conversion:
If I have a set routine that takes a value of another type, such as a
string
instead of an integer, I can convert it to whatever I want without the
user
being aware of what my internal representation is.

Matt


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

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