vb.net获取并设置问题 [英] vb.net Get and Set question
问题描述
我是OOP和vb.net的新手,到目前为止,我一直在VBA中进行编码.我真的很难理解Class Get/Set与仅使用带有私有变量的公共函数之间的区别.什么是Get/Set的海豚.下面的代码是当我需要获取私有变量时的工作.
I am new to OOP and vb.net, up till now i have been coding in VBA. I am really having a hard time understanding the difference between the Class Get/Set vs. just using a public function with a private variable. What is porpoise of Get/Set. The code below is what i do when i need to get a private variable.
Public Class Dog
Private DogRunning as Boolean
Public Function IsDogRunning()
'Find Out if Dog is Running
If DogIsRunning then
DogRunning = True
Else
DogRunning = False
End If
Return DogRunning
End Function
End Class
预先感谢您,
-J
Thank you in advance,
-J
推荐答案
我已经读到某处内容,在.NET中引入properties
(Get/Set)
之前,.NET的架构师曾考虑过是否有必要引入属性,因为可以从这些方法获得相似的功能.最后,这些属性已在.NET中引入.
据我了解,Get/Set (Property)
的目的如下:
I have read somewhere that before introducingproperties
(Get/Set)
in .NET, the architects of .NET have thought whether it is necessary to introduce properties, as the similar function can be obtained from the methods. Finally the properties have been introduced in .NET.
As I understand the purpose ofGet/Set (Property)
is as follows:
- 属性描述对象的
attribute
.比如说汽车的Color
,汽车是否具有AC
一样. -
Setter
和Getter
块被内聚地封装到对象的单个成员中,即Property
例如.
- A property describes an
attribute
of an object. SayColor
of a Car, whether the car hasAC
like so. - The
Setter
andGetter
blocks are cohesively encapsulated into a single member of the object i.e. theProperty
For eg.
Public Class Car1
Private mBodyColor As Color
Public Property BodyColor() As Color
Get
'Do some work
Return mBodyColor
End Get
Set(ByVal Value As Color)
mBodyColor = Value
'Do some validation and any other relevant work
End Set
End Property
End Class
Public Class Car4
Private mBodyColor As Color
Public Function GetBodyColor() As Color
'Do some work
Return mBodyColor
End Function
Private Sub SetBodyColor(ByVal value As Color)
mBodyColor = value
'Do some work
End Sub
End Class
在上述Car1
类中的示例中,使用了具有两种 Get and Set
方法的属性.
出于相同目的使用方法,我们必须手动命名诸如Get ...和Set ...之类的方法,以便我们可以像在Car4
类中使用的一样识别它们.因此,如果未按上述格式给出名称,则可能会丢失.
Property
指定为ReadOnly
或WriteOnly
,如下所示:
In the above example in Car1
class the property is used which has both Get and Set
methods.
Using methods for the same purpose we have to manually name the methods like Get... and Set... so that we can recognize them together as used in Car4
class. So, there is a chance of missing if the name is not given in the above format.
Property
as ReadOnly
or WriteOnly
as below:Public Class Car2
Private mBodyColor As Color
Public ReadOnly Property BodyColor() As Color
Get
Return mBodyColor
End Get
End Property
End Class
Public Class Car3
Private mBodyColor As Color
Public WriteOnly Property BodyColor() As Color
Set(ByVal Value As Color)
mBodyColor = Value
End Set
End Property
End Class
使用方法可以完成相同的事情,但是它是not intuitive
.
action
的过程中(例如Drive car,ChangeGear等),则使用一种方法,并且其中的属性如Color of汽车,具有AC等要求使用属性.
Same thing can be accomplished using the methods but it is not intuitive
.
action
is to be performed like Drive car, ChangeGear etc. then a method is used and where an attribute like Color of the car, has AC etc. are required a property is used
我认为您可能会从文档和其他参考资料中获得更详细的解释.我试图把我了解的东西放在这里
对于问题中给出的情况,IsDogRunning
仅查询Object
的state
我认为如下的Property
是合适的
I think you may get more detailed explanation from the documentation and other references. I tried to put here what I understand
For the case given in the question, IsDogRunning
only queries the state
of the Object
I think the Property
as below is suitable
Public Class Dog
Private mIsDogRunning as Boolean
Public Property IsDogRunning() as Boolean
Get
Return mIsDogRunning
End Get
Set (ByVal Value as Boolean)
mIsDogRunning = Value
End Set
End Property
End Class
内部属性是只是一对方法而已.它们基本上评估为get和set访问器方法.
您应该使用属性,除非该属性将导致一些意外的,可能长期运行的副作用,或者有其他一些充分的理由使用一种方法.
有关详细信息,建议阅读 MSDN上的《财产使用指南》 .特别是在以下情况下使用一种方法:
该操作是一个转换,例如Object.ToString.
该操作非常昂贵,您希望与用户进行交流,以使他们应考虑将结果缓存.
使用get访问器获取属性值会产生明显的副作用.
连续两次呼叫成员会产生不同的结果.
执行顺序很重要.请注意,类型的属性应能够以任意顺序设置和检索.
该成员是静态的,但返回可以更改的值.
该成员返回一个数组.
否则,我会使用一个属性.布拉德·阿布拉姆(Brad Abram)在博客中写了其他一些详细信息,包括某些API函数使用方法的充分理由(主要是因为它们可能导致跨计算机通信,这属于副作用"类别).
Properties, internally, are nothing but a pair of methods. They basically evaluate to a get and set accessor method.
You should use properties, unless the property is going to cause some unexpected, potentially long running side effect, or there is some other good reason to use a method.
For details, I suggest reading the Property Usage Guidelines on MSDN . In particular, use a method when:
The operation is a conversion, such as Object.ToString.
The operation is expensive enough that you want to communicate to the user that they should consider caching the result.
Obtaining a property value using the get accessor would have an observable side effect.
Calling the member twice in succession produces different results.
The order of execution is important. Note that a type''s properties should be able to be set and retrieved in any order.
The member is static but returns a value that can be changed.
The member returns an array.
Otherwise, I''d use a property. Brad Abram''s blogged some other details, including good reasons why certain API functions use methods (mostly because they could cause cross-computer communication, which would fall into the "side effect" category).
除了已经说过的以外,Property
确实创建了两个Functions
在幕后".
查看生成的IL(中间语言 [
Adding to what has already been said, aProperty
DOES create twoFunctions
''under the hood''.
When looking at the generated IL (Intermediate Language[^], this is the language that both VB and C# are translated into when compiled and that actually runs your application) you''ll see that aProperty
such as the following:
' Sample code from VJ Reddy's answer.
Private mBodyColor As Color
Public Property BodyColor() As Color
Get
'Do some work
Return mBodyColor
End Get
Set(ByVal Value As Color)
mBodyColor = Value
'Do some validation and any other relevant work
End Set
End Property
在IL中生成两个函数,分别是get_BodyColor
和set_BodyColor
.
因此,基本上Property
是对get and set函数的调用.
我认为,为什么应该在.NET中使用Property
而不是Public field
呢?对我来说,最重要的原因是封装 [Properties代替Functions
? Properties
迫使程序员采用一种公认的并且可以识别的编码风格,而Functions
则没有.
Generates two functions in IL, get_BodyColor
and set_BodyColor
.
So basically a Property
is a call to a get and set function.
Why you should use a Property
in .NET instead of a Public field
has already been said I think. For me the most important reason is Encapsulation[^]. If, for some reason, you would have to validate values or set some other values when a field
changes this can only be done through Properties
or Functions
. However, had you not chosen to have a Property
or Function
and only have a Public field
then this is not possible anymore (or it would require a ''breaking change''). So why Properties
instead of Functions
? Properties
force the programmer into an accepted and recognizable coding style where Functions
do not.
这篇关于vb.net获取并设置问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!