字段与属性与方法 [英] Fields vs Properties vs Methods

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

问题描述

我对属性和方法非常清楚,我认为我理解Fields但是

对此不确定。我是否在下面的课程中正确标记了项目?

任何澄清将不胜感激。谢谢。


Public Class MyClass


''这是一个字段

Public myfield as string


''这是一个属性

公共财产()为整数

......

end Property


''这是一种方法

Public Sub mysub(byval x as integer)

.... 。

结束分


结束课


-

丹尼斯在休斯顿

I am very clear on Properties and Methods and I think I understand Fields but
not sure about this. Do I have the items marked properly in the below class?
Any clarification would be appreciated. Thanks.

Public Class MyClass

''This is a field
Public myfield as string

''This is a property
Public Property () as integer
......
end Property

''This is a method
Public Sub mysub (byval x as integer)
.....
end sub

End Class

--
Dennis in Houston

推荐答案

字段是实际数据,如字符串或整数。如果它被标记为

public它可以直接从其他类访问,使用引用

你的类如MyClass.MyField = 10或dim x as integer = MyClass.MyField。


一个属性看起来像外面的世界,因为你可以

使用它从类中获取值或通过类的一些价值,但实际上在课堂上发生的事情取决于在

属性代码本身中运行的代码。大多数情况下,一个属性将用于获取或设置私有字段的内容,例如:


私有_int作为整数


公共属性Int()为整数

获取

返回_int

结束获取

设置(ByVal值为整数)

_int = value

结束集

结束物业


这看起来可能并不重要,但诀窍是,只要获得或设置了
a属性,就会运行一些代码。当你的财产发生这样的变化时,这可以让你做一些事情......


Public Property Int()as Integer

获取

返回_int

结束获取

设置(ByVal值为整数)

_int =值

OnIntegerChanged()

结束集

结束物业


物业没有''必须访问一个字段。它可能只执行一些

处理并返回当时创建的值,例如:


Public Readonly属性DateString()as String

获取

返回DateTime.Now.ToShortDateString()

结束获取

结束物业


方法只运行一大块代码。方法可能具有在调用方法时传入的参数,即


处理完成后,该方法可能会返回一个值。该方法可能会重载,这就是说,

几个同名的方法但具有不同的参数可能存在
。一个方法可能是虚拟的或可覆盖的,也就是说从某个类派生的类可以通过修改它的定义来改变方法的行为。

这是多态性的基础。

中的方法Visual Basic使用Sub或Function关键字。


-

Bob Powell [MVP]

Visual C#,System.Drawing


在Windows窗体中查找优秀的Windows窗体文章提示和技巧
http://www.bobpowell.net/tipstricks.htm


回答那些GDI +问题使用GDI +常见问题解答
http://www.bobpowell.net/faqmain。 htm


所有新文章都提供C#和VB.NET代码。

订阅所提供的RSS提要,绝不会错过任何新文章。


" Dennis" <德**** @ discussions.microsoft.com>在消息中写道

新闻:CE ********************************** @ microsof t.com ...
A field is the actual data such as a string or an integer. If it''s marked
public it can be accessed directly from other classes using a reference to
your class such as MyClass.MyField=10 or dim x as integer=MyClass.MyField.

A property looks to the outside world like its a field inasmuch as you can
use it to obtain a value from the class or pass some value to the class but
what actually goes on inside the class is dependent on code that runs in the
property code itself. Most often a property will be used to get or set the
contents of a private field such as:

Private _int as Integer

Public Property Int() as Integer
Get
return _int
End Get
Set(ByVal value as Integer)
_int=value
End Set
End Property

This may not look important but the trick is that some code is run whwnever
a property is obtained or set. This enables you to do such things as raise
an event when a property changes like this...

Public Property Int() as Integer
Get
return _int
End Get
Set(ByVal value as Integer)
_int=value
OnIntegerChanged()
End Set
End Property

A property doesn''t have to access a field. It might just perform some
processing and return a value that was created at that moment such as:

Public Readonly Property DateString() as String
Get
return DateTime.Now.ToShortDateString()
End Get
End Property

A method just runs a chunk of code. A method may have parameters which are
passed in when the method is called. The method may return a value when the
processing is finished. The method might be overloaded, this is to say that
several methods of the same name but having different parameters might
exist. A method might be virtual or overridable which is to say that classes
derived from a certain class can change the behaviour of the method by
modifying it''s definition. This is the basis of polymorphism. Methods in
Visual Basic use the Sub or Function keywords.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:CE**********************************@microsof t.com...
我对属性和方法非常清楚,我想我理解Fields
但不确定。我是否在下面的
级别中正确标记了项目?任何澄清将不胜感激。谢谢。

Public Class MyClass

''这是一个领域
公共myfield as string

''这是一个属性
公共属性()为整数
.....
结束属性

''这是一种方法
Public Sub mysub(byval x as integer) )
....
结束分类

-
丹尼斯在休斯敦
I am very clear on Properties and Methods and I think I understand Fields but not sure about this. Do I have the items marked properly in the below class? Any clarification would be appreciated. Thanks.

Public Class MyClass

''This is a field
Public myfield as string

''This is a property
Public Property () as integer
.....
end Property

''This is a method
Public Sub mysub (byval x as integer)
....
end sub

End Class

--
Dennis in Houston



字段是实际数据,如字符串或整数。如果它被标记为

public它可以直接从其他类访问,使用引用

你的类如MyClass.MyField = 10或dim x as integer = MyClass.MyField。


一个属性看起来像外面的世界,因为你可以

使用它从类中获取值或通过类的一些价值,但实际上在课堂上发生的事情取决于在

属性代码本身中运行的代码。大多数情况下,一个属性将用于获取或设置私有字段的内容,例如:


私有_int作为整数


公共属性Int()为整数

获取

返回_int

结束获取

设置(ByVal值为整数)

_int = value

结束集

结束物业


这看起来可能并不重要,但诀窍是,只要获得或设置了
a属性,就会运行一些代码。当你的财产发生这样的变化时,这可以让你做一些事情......


Public Property Int()as Integer

获取

返回_int

结束获取

设置(ByVal值为整数)

_int =值

OnIntegerChanged()

结束集

结束物业


物业没有''必须访问一个字段。它可能只执行一些

处理并返回当时创建的值,例如:


Public Readonly属性DateString()as String

获取

返回DateTime.Now.ToShortDateString()

结束获取

结束物业


方法只运行一大块代码。方法可能具有在调用方法时传入的参数,即


处理完成后,该方法可能会返回一个值。该方法可能会重载,这就是说,

几个同名的方法但具有不同的参数可能存在
。一个方法可能是虚拟的或可覆盖的,也就是说从某个类派生的类可以通过修改它的定义来改变方法的行为。

这是多态性的基础。

中的方法Visual Basic使用Sub或Function关键字。


-

Bob Powell [MVP]

Visual C#,System.Drawing


在Windows窗体中查找优秀的Windows窗体文章提示和技巧
http://www.bobpowell.net/tipstricks.htm


回答那些GDI +问题使用GDI +常见问题解答
http://www.bobpowell.net/faqmain。 htm


所有新文章都提供C#和VB.NET代码。

订阅所提供的RSS提要,绝不会错过任何新文章。


" Dennis" <德**** @ discussions.microsoft.com>在消息中写道

新闻:CE ********************************** @ microsof t.com ...
A field is the actual data such as a string or an integer. If it''s marked
public it can be accessed directly from other classes using a reference to
your class such as MyClass.MyField=10 or dim x as integer=MyClass.MyField.

A property looks to the outside world like its a field inasmuch as you can
use it to obtain a value from the class or pass some value to the class but
what actually goes on inside the class is dependent on code that runs in the
property code itself. Most often a property will be used to get or set the
contents of a private field such as:

Private _int as Integer

Public Property Int() as Integer
Get
return _int
End Get
Set(ByVal value as Integer)
_int=value
End Set
End Property

This may not look important but the trick is that some code is run whwnever
a property is obtained or set. This enables you to do such things as raise
an event when a property changes like this...

Public Property Int() as Integer
Get
return _int
End Get
Set(ByVal value as Integer)
_int=value
OnIntegerChanged()
End Set
End Property

A property doesn''t have to access a field. It might just perform some
processing and return a value that was created at that moment such as:

Public Readonly Property DateString() as String
Get
return DateTime.Now.ToShortDateString()
End Get
End Property

A method just runs a chunk of code. A method may have parameters which are
passed in when the method is called. The method may return a value when the
processing is finished. The method might be overloaded, this is to say that
several methods of the same name but having different parameters might
exist. A method might be virtual or overridable which is to say that classes
derived from a certain class can change the behaviour of the method by
modifying it''s definition. This is the basis of polymorphism. Methods in
Visual Basic use the Sub or Function keywords.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

"Dennis" <De****@discussions.microsoft.com> wrote in message
news:CE**********************************@microsof t.com...
我对属性和方法非常清楚,我想我理解Fields
但不确定。我是否在下面的
级别中正确标记了项目?任何澄清将不胜感激。谢谢。

Public Class MyClass

''这是一个领域
公共myfield as string

''这是一个属性
公共属性()为整数
.....
结束属性

''这是一种方法
Public Sub mysub(byval x as integer) )
....
结束分类

-
丹尼斯在休斯敦
I am very clear on Properties and Methods and I think I understand Fields but not sure about this. Do I have the items marked properly in the below class? Any clarification would be appreciated. Thanks.

Public Class MyClass

''This is a field
Public myfield as string

''This is a property
Public Property () as integer
.....
end Property

''This is a method
Public Sub mysub (byval x as integer)
....
end sub

End Class

--
Dennis in Houston



Bob,


什么是属性或方法,不是关于

定义的问题对于字段。在这种情况下,我不知道是否有一般的

定义。所以我没有回答,好奇我是否有人会带来这个。


对丹尼斯来说,不是为了回答,字段是一个非常古老的定义。

当你在过去看到记录或记忆描述图形时,它总是这个


| Field1 | Field2 | Field3 | etc


当然它仍然是内存中的一个字段,但不像以前那么具体

它真的是真的永远memory_adress(长度)


为了现在描述它我主要是尝试在内存中使用

对象或价值

在数据行描述中

项目


也许这会以某种方式清除它


Cor
Bob,

What a property or a method is, was not the question it goes about the
definition for "field". I do not know if there is for that a general
definition in this case. Therefore I did not answer it, curious that I was
if somebody would come with that.

And to Dennis than as a try for an answer, a field is a very old definition.
When you saw in past a record or a memory description graphical it was
always this

|Field1|Field2|Field3|etc

It is of course still a field in memory, however not so concrete as in past
where it was really forever memory_adress(length)

To describe it now I mostly "try" to use for in memory
Object or Value
In a data row description
Item

Maybe this clears it somehow

Cor


这篇关于字段与属性与方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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