继承问题/困惑 [英] inheritance problem/confusion

查看:81
本文介绍了继承问题/困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这是我班级的结构。

" class Global

" |

" -------------------------------------------

| |

" class SomeForm1 class SomeForm2

现在我在Global中声明一个int并在主要的初始化

Someform1。当我尝试从SomeForm2访问此int时,它忽略了我对SomeForm1对此int所做的更改。


如何才能拥有真正的全局变量可以在任何地方修改

,无需担心这些

实例?


希望这一点很清楚。

Ok, this is the structure of my classes.
" class Global
" |
" -------------------------------------------
" | |
"class SomeForm1 class SomeForm2
Now I declare an int in "Global" and initialize in the main of
Someform1. When I try to access this int from SomeForm2 it ignores the
changes I made to this int from SomeForm1.

What should I do to have a true global variable that can be modified
anywhere and accessed anywhere without having to worry about these
instances?

hope this is clear.

推荐答案



" vidalsasoon" < KJ ***** @ gmail.com>在消息中写道

新闻:11 ********************* @ c13g2000cwb.googlegro ups.com ...

"vidalsasoon" <kj*****@gmail.com> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.com...
好的,这是我班级的结构。

" class Global
|
-------------------------------------------
" | |
类SomeForm1类SomeForm2

现在我在全局中声明一个int并初步在
Someform1的主要部分。当我尝试从SomeForm2访问此int时,它忽略了我对SomeForm1对此int所做的更改。

我该怎么做才能拥有一个可以修改的真正的全局变量随时随地访问任何地方而不必担心这些实例?

希望这很清楚。
Ok, this is the structure of my classes.
" class Global
" |
" -------------------------------------------
" | |
"class SomeForm1 class SomeForm2
Now I declare an int in "Global" and initialize in the main of
Someform1. When I try to access this int from SomeForm2 it ignores the
changes I made to this int from SomeForm1.

What should I do to have a true global variable that can be modified
anywhere and accessed anywhere without having to worry about these
instances?

hope this is clear.




它不是''完全清楚,因为我无法判断SomeFormN是否是子类或

实例。


无论如何,制作字段的方法具有相同的值在所有实例中,

是将其声明为静态。



It isn''t entirely clear, because I can''t tell if SomeFormN are subclasses or
instances.

Regardless, the way to make a field have the same value among all instances
is to declare it static.


我从来没有清楚地知道为什么要使用静态。回答这一切

是有道理的(而且效果很好)。


ty

I never had a clear idea on why to use static. with that answer it all
makes sense (and works perfectly).

ty


你通过main(或者它是contstructor?)来完成的启动

SomeForm1。现在这个值不能从SomeForm2中加入,因为SomeForm2是Gobal的子类而不是SomeForm1。


要访问此值,您需要创建一个

SomeForm2中的SomeForm1实例。


我希望您尝试创建SomeForm1和SomeForm2的实例并使用

值由SomeForm2的SomeForm1初始化。这里int有对象

范围,所以每个实例都有它的int副本。如果u wana与所有实例共享,你需要声明

int为静态。


" vidalsasoon"写道:
The initilization you have done thru the main(or is it the contstructor?) of
SomeForm1. Now this value cannot be accecced from SomeForm2 as SomeForm2 is a
sub class of Gobal not of SomeForm1.

For accessing this value, you need to create an instance of SomeForm1 within
SomeForm2.

I hope you are trying to create instances of SomeForm1 and SomeForm2 and use
the value initialized by SomeForm1 from SomeForm2. Here the int has object
scope, so every instance will have its on copy of int . You need to declare
the int as static if u wana share with all instances.

"vidalsasoon" wrote:
我从来没有清楚地知道为什么要使用静态。有了这个答案,这一切都是有意义的(而且效果很好)。

ty
I never had a clear idea on why to use static. with that answer it all
makes sense (and works perfectly).

ty



这篇关于继承问题/困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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