静态变量和构造函数 [英] Static variable and constructor

查看:88
本文介绍了静态变量和构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以改变静态变量的值。和

"实例变量"通过标准构造函数这意味着

这样的东西会编译:


公共类SomeClass

{

public SomeClass()

{

abc ++; //实例变量

xyz ++; //静态变量

}

int abc;

static int xyz;

}


但是,如果不使用标准构造函数我使用静态

构造函数,我只能初始化静态变量。得到的结果是,我总是看着他们自己的世界上的静态成员

完全与实例类世界分离。但是现在看起来像

静态变量对我来说是变性的。


我错过了什么吗?我是唯一一个认为这种行为是善意的人吗?b $ b违反规定?

谢谢。

解决方案

当然你可以从实例上下文访问静态上下文。

如果某些东西被声明为静态,它绝不意味着它只能是



a静态上下文访问。如果是,那么静态成员的基本目的就是丢失了。


但是,从静态上下文中,您无法访问实例成员。

实际上,这是非常合乎逻辑的。我们会引用哪个实例?

是的,您可以编写xyz ++。但是,你不能写这个.xyz ++,但是你可以写b / b
this.abc ++,因为它是一个实例成员,因此是在
的背景下
''this'',其中xyz不是。

当您更改abc变量时,该值在

范围内更改为特定实例。

当您更改xyz变量时,值会在全局

范围内更改。


是的,你是是的,静态成员与实例完全分开

成员。

但是你仍然可以从任何地方到达它们。否则,只有

没有静态成员。


希望我在这个问题上寻求帮助。

-

问候,

DennisJDMyrén

Oslo Kodebureau

" Rene" <无**** @ nospam.nospam>在消息中写道

新闻:eH ************** @ tk2msftngp13.phx.gbl ...

我知道我可以改变静态变量的值。和/或实例变量通过标准构造函数这意味着
这样的东西将编译:

公共类SomeClass
{
public SomeClass()
{
ABC ++; //实例变量
xyz ++; //静态变量
}
int abc;
static int xyz;
}
然而,如果不使用标准构造函数我使用静态
构造函数,我只能初始化静态变量。
让我感觉到的是,我总是在自己的世界中看静态成员
完全与实例类世界分离。但现在看来
静态变量对我来说是变性的。

我错过了什么吗?我是唯一一个认为这种行为违反规则的人吗?
谢谢。



我想它会使如果所需的语法是什么,对我来说更有意义

喜欢:


SomeClass。 xyz ++;


不仅仅是简单的xyz ++,只是调用xyz ++似乎不是典型的

静态调用。

"丹尼斯·梅伦 <德**** @ oslokb.no>在消息中写道

新闻:Il ******************** @ news4.e.nsc.no ...

当然,您可以从实例上下文访问静态上下文。
如果某些内容被声明为静态,那么它绝不意味着它只能从静态上下文中访问。如果是这样,那么静态成员的基本目的就会丢失。

从静态环境来看,你无法访问实例成员。
实际上,这是非常合乎逻辑的。我们会在哪个实例中引用

是的,您可以编写xyz ++。但是,你不能写这个.xyz ++,但是你可以写这个.abc ++,因为它是一个实例成员,因此在
''this'的上下文中,其中xyz不是。
当您更改abc变量时,该值在该特定实例的范围内更改。
当您更改xyz变量时,该值在全局变化
范围。

是的,你是对的,静态成员完全与实例成员分开。
但你仍然可以从任何地方联系到他们。否则,静态会员就没有意义了。

希望我能在这个问题上寻求帮助。

-
问候,
DennisJDMyrén
Oslo Kodebureau
Rene <无**** @ nospam.nospam>在消息中写道
新闻:eH ************** @ tk2msftngp13.phx.gbl ...

我知道我可以改变价值观静态变量和/或实例变量通过标准构造函数这意味着
这样的东西将编译:

公共类SomeClass
{
public SomeClass()
{
ABC ++; //实例变量
xyz ++; //静态变量
}
int abc;
static int xyz;
}
然而,如果不使用标准构造函数我使用静态
构造函数,我只能初始化静态变量。
让我感觉到的是,我总是在他们自己完全与实例类世界分离的世界中看静态成员。但现在它看起来像静态变量对我来说是变性人。

我错过了什么吗?我是唯一一个认为这种行为违反规则的人吗?
谢谢。




嗯,在这种情况下指定类是superfluos,因为你已经

那里。

如果,从另一个类,你想改变xyz,那么是的,您将需要

课程

需要使用完全限定名称,即[NAMESPACE(S)]。SomeClass。

xyz ++;


-

问候,

DennisJDMyrén

奥斯陆Kodebureau

Rene <无**** @ nospam.nospam>在留言中写道

新闻:%2 **************** @ TK2MSFTNGP09.phx.gbl ...

我猜如果所需的语法类似于:

SomeClass,那对我来说会更有意义。 xyz ++;

不仅仅是普通的xyz ++,只是调用xyz ++似乎不是你典型的静态调用。

"DennisMyrén" <德**** @ oslokb.no>在消息中写道
新闻:Il ******************** @ news4.e.nsc.no ...

当然,您可以从实例上下文访问静态上下文。
如果某些内容被声明为静态,那么它绝不意味着它只能从静态上下文中访问。如果是这样,那么静态成员的基本目的就会丢失。

从静态环境来看,你无法访问实例成员。
实际上,这是非常合乎逻辑的。我们会在哪个实例中引用

是的,您可以编写xyz ++。但是,你不能写这个.xyz ++,但是你可以写这个.abc ++,因为它是一个实例成员,因此在
''this'的上下文中,其中xyz不是。
当您更改abc变量时,该值在该特定实例的范围内更改。
当您更改xyz变量时,该值在全局变化
范围。

是的,你是对的,静态成员与
实例成员完全分开。
但是你仍然可以从任何地方到达它们。否则,静态会员就没有意义了。

希望我能在这个问题上寻求帮助。

-
问候,
DennisJDMyrén
Oslo Kodebureau
Rene <无**** @ nospam.nospam>在消息中写道
新闻:eH ************** @ tk2msftngp13.phx.gbl ...

我知道我可以改变价值观静态变量和/或实例变量通过标准构造函数这意味着
这样的东西将编译:

公共类SomeClass
{
public SomeClass()
{
ABC ++; //实例变量
xyz ++; //静态变量
}
int abc;
static int xyz;
}
然而,如果不使用标准构造函数我使用静态
构造函数,我只能初始化静态变量。
让我感觉到的是,我总是在他们自己完全与实例类世界分离的世界中看静态成员。但现在它看起来像静态变量对我来说是变性人。

我错过了什么吗?我是唯一一个认为这种行为违反规则的人吗?
谢谢。





I jus realized that I can change the values of "static variables" and
"instance variable" through the standard constructor This means that
something like this will compile:

public class SomeClass
{
public SomeClass()
{
abc++; // Instance Variable
xyz++; // Static Variable
}
int abc;
static int xyz;
}

However, if instead of using the standard constructor I use a static
constructor, I can only initialize the static variables. The thing that gets
me, is that I always looked at static members on a world of their own
totally separated from the instance class world. But now it looks like the
static variable is going transsexual on me.

Am I missing something? Am I the only one that thinks this behavior is kind
of breaking the rules?
Thanks.

解决方案

Of course you can access the static context from the instance context.
If something is declared static, it does not in any way mean it can only be
accessed from
a static context. If it was, then a fundamental purpose of static members
have been lost.

From a static context however, you cannot access instance members.
Actually, that is very logical. Which instance would we otherwise refer to?
Yes, you can write xyz++. However, you cannot write this.xyz++, but you can
write
this.abc++, since it is an instance member, and thus is in context of
''this'', where xyz is not.
When you are changing the abc variable, the value is changed in the scope of
that particular instance.
When you are changing the xyz variable, the value is changed in a global
scope.

Yes, you are right, static members are totally separated from the instance
members.
But you can still reach them from anywhere. Otherwise, there would be just
no point in having static members.

Hope i have been for help in this question.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Rene" <no****@nospam.nospam> wrote in message
news:eH**************@tk2msftngp13.phx.gbl...

I jus realized that I can change the values of "static variables" and
"instance variable" through the standard constructor This means that
something like this will compile:

public class SomeClass
{
public SomeClass()
{
abc++; // Instance Variable
xyz++; // Static Variable
}
int abc;
static int xyz;
}

However, if instead of using the standard constructor I use a static
constructor, I can only initialize the static variables. The thing that
gets me, is that I always looked at static members on a world of their own
totally separated from the instance class world. But now it looks like the
static variable is going transsexual on me.

Am I missing something? Am I the only one that thinks this behavior is
kind of breaking the rules?
Thanks.



I guess it would make more sense to me if the required syntax was something
like:

SomeClass. xyz++;

not just the plain xyz++, just calling xyz++ does not seem like your typical
static call.
"Dennis Myrén" <de****@oslokb.no> wrote in message
news:Il********************@news4.e.nsc.no...

Of course you can access the static context from the instance context.
If something is declared static, it does not in any way mean it can only
be accessed from
a static context. If it was, then a fundamental purpose of static members
have been lost.

From a static context however, you cannot access instance members.
Actually, that is very logical. Which instance would we otherwise refer
to?
Yes, you can write xyz++. However, you cannot write this.xyz++, but you
can write
this.abc++, since it is an instance member, and thus is in context of
''this'', where xyz is not.
When you are changing the abc variable, the value is changed in the scope
of that particular instance.
When you are changing the xyz variable, the value is changed in a global
scope.

Yes, you are right, static members are totally separated from the instance
members.
But you can still reach them from anywhere. Otherwise, there would be just
no point in having static members.

Hope i have been for help in this question.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Rene" <no****@nospam.nospam> wrote in message
news:eH**************@tk2msftngp13.phx.gbl...

I jus realized that I can change the values of "static variables" and
"instance variable" through the standard constructor This means that
something like this will compile:

public class SomeClass
{
public SomeClass()
{
abc++; // Instance Variable
xyz++; // Static Variable
}
int abc;
static int xyz;
}

However, if instead of using the standard constructor I use a static
constructor, I can only initialize the static variables. The thing that
gets me, is that I always looked at static members on a world of their
own totally separated from the instance class world. But now it looks
like the static variable is going transsexual on me.

Am I missing something? Am I the only one that thinks this behavior is
kind of breaking the rules?
Thanks.




Well, specifying the class in this case is superfluos, since you are already
there.
If, from another class, you would like to change xyz, then yes you will of
course
need to use the fully qualified name, which is [NAMESPACE(S)].SomeClass.
xyz++;

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Rene" <no****@nospam.nospam> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...

I guess it would make more sense to me if the required syntax was something
like:

SomeClass. xyz++;

not just the plain xyz++, just calling xyz++ does not seem like your
typical static call.
"Dennis Myrén" <de****@oslokb.no> wrote in message
news:Il********************@news4.e.nsc.no...

Of course you can access the static context from the instance context.
If something is declared static, it does not in any way mean it can only
be accessed from
a static context. If it was, then a fundamental purpose of static members
have been lost.

From a static context however, you cannot access instance members.
Actually, that is very logical. Which instance would we otherwise refer
to?
Yes, you can write xyz++. However, you cannot write this.xyz++, but you
can write
this.abc++, since it is an instance member, and thus is in context of
''this'', where xyz is not.
When you are changing the abc variable, the value is changed in the scope
of that particular instance.
When you are changing the xyz variable, the value is changed in a global
scope.

Yes, you are right, static members are totally separated from the
instance members.
But you can still reach them from anywhere. Otherwise, there would be
just no point in having static members.

Hope i have been for help in this question.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Rene" <no****@nospam.nospam> wrote in message
news:eH**************@tk2msftngp13.phx.gbl...

I jus realized that I can change the values of "static variables" and
"instance variable" through the standard constructor This means that
something like this will compile:

public class SomeClass
{
public SomeClass()
{
abc++; // Instance Variable
xyz++; // Static Variable
}
int abc;
static int xyz;
}

However, if instead of using the standard constructor I use a static
constructor, I can only initialize the static variables. The thing that
gets me, is that I always looked at static members on a world of their
own totally separated from the instance class world. But now it looks
like the static variable is going transsexual on me.

Am I missing something? Am I the only one that thinks this behavior is
kind of breaking the rules?
Thanks.





这篇关于静态变量和构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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