Field vs Static Field,基本问题...... [英] Field vs Static Field, basic question...

查看:167
本文介绍了Field vs Static Field,基本问题......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望这是一个发布真正初学者的好地方。


基本上我需要知道字段和静态字段之间的区别。


我的书sux并没有解释清楚。似乎一个字段的范围相同,并且可以做同样的事情。

IE字段是在一个类而不是一个方法中声明的,所以

类可以使用它。

我的书中描述的静态字段是相同的吗?


我必须遗漏一些东西...

I hope this is an ok place to post real beginner stuff.

Basically I need to know the difference between a field, and a static field.

My book sux and doesn''t explain this well. It seems that a field is scoped
the same and can do the same things.
IE a field is declared in a class instead of a method so all methods of the
class can use it.
And a static field is described in my book as the same?

I must be missing something...


推荐答案

学习C#< so ***** @ microsoft.com>写道:
Learning C# <so*****@microsoft.com> wrote:
我希望这是一个发布真正初学者的好地方。

基本上我需要知道字段和静态字段之间的区别。

我的书sux并没有解释清楚。似乎一个字段的范围相同,并且可以做同样的事情。
IE字段是在类而不是方法中声明的,因此
类的所有方法都可以使用它。
静态字段在我的书中描述的相同吗?

我一定会遗漏一些东西......
I hope this is an ok place to post real beginner stuff.

Basically I need to know the difference between a field, and a static field.

My book sux and doesn''t explain this well. It seems that a field is scoped
the same and can do the same things.
IE a field is declared in a class instead of a method so all methods of the
class can use it.
And a static field is described in my book as the same?

I must be missing something...




它与静态方法和实例

方法,静态属性和实例属性之间的区别相同。


任何静态是一般的类型,而不是任何特定的类型的b $ b实例。 (例如,您可以使用静态字段来计算已创建了多少个b $ b实例。)


任何非静态的特定于该特定实例的。 (你可以

使用一个实例字段来说,例如,这个实例是< n>创建了一个

。)


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复该群组,请不要给我发邮件太



It''s the same difference as between a static method and an instance
method, and a static property and an instance property.

Anything "static" is to do with the type in general, not any particular
instance of the type. (You might use a static field to count how many
instances have been created, for example.)

Anything non-static is specific to that particular instance. (You might
use an instance field to say, "This instance was the <n>th one to be
created, for example.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too




" Learning C#" <所以***** @ microsoft.com>在留言中写道

新闻:Ze ******************** @ comcast.com ...

"Learning C#" <so*****@microsoft.com> wrote in message
news:Ze********************@comcast.com...
我希望这是一个发布真正的初学者的好地方。

基本上我需要知道字段和静态
字段之间的区别。
我的书sux并且没有'好好解释一下。似乎一个字段的范围相同并且可以做同样的事情。
IE字段是在类而不是方法中声明的,因此类的
的所有方法都可以使用它。
在我的书中描述的静态字段是相同的吗?

我必须遗漏一些东西......
I hope this is an ok place to post real beginner stuff.

Basically I need to know the difference between a field, and a static field.
My book sux and doesn''t explain this well. It seems that a field is scoped
the same and can do the same things.
IE a field is declared in a class instead of a method so all methods of the class can use it.
And a static field is described in my book as the same?

I must be missing something...




A字段(根据我的理解,是没有财产声明的财产

(公共变量))。

除此之外,适用于财产的所有内容都适用于字段

(静态vs实例等......)




公共类x

{

private int m_x; // private var x

public int y; // field var y

private static int m_X; // private static var X

public static int Y; //静态字段Y


public int x //实例属性

{

set

{

返回m_x;

}

get

{

m_x = value ;

}


public static int X //静态属性

{

get

{

返回m_X;

}

设定

{

m_X =价值;

}

}



A field (from what I understand is a property without a property declaration
(public variable)).
Apart from that everything that applies to properties applies to fields
(static vs instance etc...)

ie
public class x
{
private int m_x; //private var x
public int y; //field var y
private static int m_X; //private static var X
public static int Y; //static field Y

public int x //instance property
{
set
{
return m_x;
}
get
{
m_x = value;
}

public static int X //static property
{
get
{
return m_X;
}
set
{
m_X = value;
}
}


John Baro< jo *** @ NOSPAMmesware .com.au>写道:
John Baro <jo***@NOSPAMmesware.com.au> wrote:
一个字段(据我所知是一个没有属性声明的属性
(公共变量))。


编号字段是变量。一个属性根本不需要变量

支持它。

除了适用于属性的所有内容都适用于字段
(静态vs实例等...)
A field (from what I understand is a property without a property declaration
(public variable)).
No. A field is a variable. A property doesn''t have to have a variable
backing it at all.
Apart from that everything that applies to properties applies to fields
(static vs instance etc...)




好​​吧,很多东西,但不是一切。例如,一个属性

不能通过引用传递。


属性本质上是一种方法(或一对方法)和一点点

元数据允许使用类似字段的语法访问这些方法。


但是,静态的意思是静态。对于所有会员类型都是一样的。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复该组,请不要给我发邮件



Well, lots of things, but not everything. For instance, a property
can''t be passed by reference.

A property is essentially a method (or pair of methods) and a bit of
metadata to allow those methods to be accessed using field-like syntax.

But yes, the meaning of "static" is the same for all member types.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


这篇关于Field vs Static Field,基本问题......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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