使用类实例化声明的静态变量 [英] static vars declared with class instantiation

查看:159
本文介绍了使用类实例化声明的静态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有Class1包含


静态Class2 var1 = new Class2();


如果var1只执行Class2构造函数代码在
代码执行路径中引用?


或者是在引用的任何类的加载时执行的Class2构造函数代码

Class1?


或者...别的...

Say I have Class1 which contains

static Class2 var1 = new Class2();

Is Class2 constructor code only executed if var1 is referenced in the
code-execution path ?

Or is Class2 constructor code executed on load of any class that references
Class1 ?

Or ... something else ...

推荐答案



John A Grandy写道:

John A Grandy wrote:
说我有Class1包含

静态Class2 var1 = new Class2();

如果在代码执行路径中引用了var1,那么是否只执行了Class2构造函数代码?

或者是在引用
Class1的任何类的加载时执行的Class2构造函数代码? />
或......还有别的...
Say I have Class1 which contains

static Class2 var1 = new Class2();

Is Class2 constructor code only executed if var1 is referenced in the
code-execution path ?

Or is Class2 constructor code executed on load of any class that references
Class1 ?

Or ... something else ...




我当然假设代码看起来像这样:


public Class1

{

静态Class2 var1 = new Class2();

}


这样var1实际上是Class1的静态成员。


Jon Skeet是实例化大师,但是如果内存服务于实例,那么将构造
的Class2并将其分配给var1第一次执行路径提到Class1,或者通过尝试创建

一个实例或者提到Class1的任何静态成员。


它与为Class1创建静态构造函数相同:静态

构造函数在第一个提及时运行。 Class1之前,可以访问

类中的任何内容。


我保留错误的权利。 :-)



I''m assuming, of course, that the code looks something like this:

public Class1
{
static Class2 var1 = new Class2();
}

So that "var1" is effectively a static member of Class1.

Jon Skeet is the instantiation guru, but if memory serves the instance
of Class2 will be constructed and assigned to var1 the first time that
the execution path makes mention of Class1, either by trying to create
an instance or mentioning any static member of Class1.

It''s the same as creating a static constructor for Class1: the static
constructor runs on first "mention" of Class1, before anything in the
class is accessible.

I reserve the right to be wrong, though. :-)


嗯...


所以,我在这里猜测,但似乎与Java相比,单身人士可能要少得多吗?

与Java相比?


带有静态构造函数的.NET类(或者它的全部内容)包含在

静态变量中的数据(在声明中实例化)有效地替换了一个

Singleton ...因为任何与实例化相关的工作都保证只有

执行一次 - 在课程的第一次参考时。


" Bruce Wood" <峰; br ******* @ canada.com>在消息中写道

news:11 ********************** @ f6g2000cwb.googlegro ups.com ...
Hmmm ...

So, I''m kindof guessing here, but seems that Singletons might be much less
relevant in .NET compared with , say , Java ?

A .NET class with a static constructor (or has all its data contained in
static vars that are instantiated on declaration) effectively replaces a
Singleton ... because any instantiation-related work is guaranteed to only
be performed once -- on first reference of the class.

"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@f6g2000cwb.googlegro ups.com...

John A Grandy写道:

John A Grandy wrote:
说我有Class1,其中包含

静态Class2 var1 = new Class2();

或者是在任何类的负载上执行的Class2构造函数代码
引用
Class1?

或者......别的...
Say I have Class1 which contains

static Class2 var1 = new Class2();

Is Class2 constructor code only executed if var1 is referenced in the
code-execution path ?

Or is Class2 constructor code executed on load of any class that
references
Class1 ?

Or ... something else ...



我当然假设代码看起来像这样:

公共Class1
{静态Class2 var1 = new Class2();

}

这样var1 ;实际上是Class1的静态成员。

Jon Skeet是实例化大师,但是如果内存服务于实例,那么第2次将构造并分配给var1
执行路径提到了Class1,要么试图创建一个实例,要么提到Class1的任何静态成员。

它与为Class1创建静态构造函数相同: static
构造函数首先在提及中运行。 Class1之前,可以访问
课程中的任何内容。

我保留错误的权利。 : - )



I''m assuming, of course, that the code looks something like this:

public Class1
{
static Class2 var1 = new Class2();
}

So that "var1" is effectively a static member of Class1.

Jon Skeet is the instantiation guru, but if memory serves the instance
of Class2 will be constructed and assigned to var1 the first time that
the execution path makes mention of Class1, either by trying to create
an instance or mentioning any static member of Class1.

It''s the same as creating a static constructor for Class1: the static
constructor runs on first "mention" of Class1, before anything in the
class is accessible.

I reserve the right to be wrong, though. :-)



毫不奇怪...... Jon在这里写了一篇关于它的文章
http://www.yoda.arachsys.com/csharp/...fieldinit.html


干杯,


Greg Young

MVP - C#
http://geekswithblogs.net/gyoung

" Bruce Wood" <峰; br ******* @ canada.com>在消息中写道

news:11 ********************** @ f6g2000cwb.googlegro ups.com ...
Not surprisingly .. Jon has a write up about it here
http://www.yoda.arachsys.com/csharp/...fieldinit.html

Cheers,

Greg Young
MVP - C#
http://geekswithblogs.net/gyoung
"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@f6g2000cwb.googlegro ups.com...

John A Grandy写道:

John A Grandy wrote:
说我有Class1,其中包含

静态Class2 var1 = new Class2();

或者是在任何类的负载上执行的Class2构造函数代码
引用
Class1?

或者......别的...
Say I have Class1 which contains

static Class2 var1 = new Class2();

Is Class2 constructor code only executed if var1 is referenced in the
code-execution path ?

Or is Class2 constructor code executed on load of any class that
references
Class1 ?

Or ... something else ...



我当然假设代码看起来像这样:

公共Class1
{静态Class2 var1 = new Class2();

}

这样var1 ;实际上是Class1的静态成员。

Jon Skeet是实例化大师,但是如果内存服务于实例,那么第2次将构造并分配给var1
执行路径提到了Class1,要么试图创建一个实例,要么提到Class1的任何静态成员。

它与为Class1创建静态构造函数相同: static
构造函数首先在提及中运行。 Class1之前,可以访问
课程中的任何内容。

我保留错误的权利。 : - )



I''m assuming, of course, that the code looks something like this:

public Class1
{
static Class2 var1 = new Class2();
}

So that "var1" is effectively a static member of Class1.

Jon Skeet is the instantiation guru, but if memory serves the instance
of Class2 will be constructed and assigned to var1 the first time that
the execution path makes mention of Class1, either by trying to create
an instance or mentioning any static member of Class1.

It''s the same as creating a static constructor for Class1: the static
constructor runs on first "mention" of Class1, before anything in the
class is accessible.

I reserve the right to be wrong, though. :-)



这篇关于使用类实例化声明的静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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