为什么使用属性? [英] Why use properties?

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

问题描述

我看到大量的例子,其中人们使用属性,其中get和set

直接对应于类中的单个变量。如果是这种情况,为什么

不能直接访问变量?这是封装问题吗?这个

看起来很奇怪,因为

属性的级别通常没有限制。只是curoius ...


-Ben

I see tons of examples where people use properties where the get and set
correspond directly to a single variable in a class. If this is the case, why
not just access the variable directly? Is it an encapsulation issue? This
seems strange since there''s often no restriction applied at the level of the
property. Just curoius...

-Ben

推荐答案

嗨Ben,

这是一个很好的OOP练习。你可能想要在
设置一个值或执行一些其他操作之前执行一些验证,或者为get / set访问器设置不同的访问级别

(暂时不可能)。 NET 1.0 / 1.1)。即使

还没有适用,代码也会为将来的更改做好准备。它变成了一个

的习惯,所以你会看到很多类似的代码。


-


Best问候,


Carlos J. Quintero


MZ-Tools:Visual Studio .NET,VB6,VB5和VBA的生产力加载项

您可以更快地编码,设计和记录。

加载项开发人员的免费资源:
http://www.mztools.com


Ben R. <是** @ newsgroup.nospam> escribiónenel mensaje

news:71 ********************************** @ microsof t.com ...
Hi Ben,

It is a good OOP practice. You may want to perform some validation before
setting a value or perform some other action, or set different access levels
to the get/set accessors (not possible yet in .NET 1.0/1.1). Even if none of
this applies yet, the code is prepared for future changes. It becomes an
habit, so you see a lot of code like that.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com

"Ben R." <be**@newsgroup.nospam> escribió en el mensaje
news:71**********************************@microsof t.com...
我看到很多例子,其中人们使用属性,其中get和set
直接对应于类中的单个变量。如果是这种情况,
为什么不直接访问变量?这是封装问题吗?这似乎很奇怪,因为在
属性层面上通常没有任何限制。只是curoius ...

-Ben
I see tons of examples where people use properties where the get and set
correspond directly to a single variable in a class. If this is the case,
why
not just access the variable directly? Is it an encapsulation issue? This
seems strange since there''s often no restriction applied at the level of
the
property. Just curoius...

-Ben



封装和验证是一些问题。你可能还需要根据父属性设置

偶然/因变量/属性,或者在设置时运行

a方法,甚至根据新的< br $>
值。


以这种方式设置属性是一个好习惯,因为它经常是你需要的b / b
/要求将随着您的应用程序而变化

的发展......可扩展性应该始终是一个考虑因素。并且为了维护

目的,保持一致意味着不太容易出错并且更明确

意图。


hth,


史蒂夫

" Ben R." <是** @ newsgroup.nospam>在消息中写道

news:71 ********************************** @ microsof t.com ...

|我看到大量的例子,人们使用获取和设置的属性

|直接对应于类中的单个变量。如果是这样的话,

为什么

|不只是直接访问变量?这是封装问题吗?这个

|看起来很奇怪,因为

的水平通常没有限制。

|属性。只是curoius ...

|

| -Ben
encapsulation and validation are some issues. you may also need to set
contingent/dependant variables/properties based on a parent property or run
a method when it is set or even raise different events based on its new
value.

it is a good habit to set up properties in this fashion since it is often
the case that your needs/requirements will change as your application
evolves...scalability should always be a consideration. and for maintanence
purposes, being consistent means being less prone to error and more clear in
intent.

hth,

steve
"Ben R." <be**@newsgroup.nospam> wrote in message
news:71**********************************@microsof t.com...
|I see tons of examples where people use properties where the get and set
| correspond directly to a single variable in a class. If this is the case,
why
| not just access the variable directly? Is it an encapsulation issue? This
| seems strange since there''s often no restriction applied at the level of
the
| property. Just curoius...
|
| -Ben




" Ben R." <是** @ newsgroup.nospam>在消息中写道

news:71 ********************************** @ microsof t.com ...

"Ben R." <be**@newsgroup.nospam> wrote in message
news:71**********************************@microsof t.com...
我看到大量的例子,其中人们使用属性,其中get和set
直接对应于类中的单个变量。如果是这种情况,
为什么不直接访问变量?这是封装问题吗?这似乎很奇怪,因为在
属性层面上通常没有任何限制。只是curoius ...

-Ben
I see tons of examples where people use properties where the get and set
correspond directly to a single variable in a class. If this is the case,
why
not just access the variable directly? Is it an encapsulation issue? This
seems strange since there''s often no restriction applied at the level of
the
property. Just curoius...

-Ben




为了配合所有其他建议,我有一个问题要求你

问题:


为什么不呢?


Mythran



To go along with all the other suggestions, I have a question for your
question:

Why not?

Mythran


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

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