继承问题/困惑

编程入门 行业动态 更新时间:2024-10-21 11:59:01
本文介绍了继承问题/困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

好的,这是我班级的结构。 " 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>在消息中写道 新闻:11 ********************* @ c13g2000cwb.googlegro ups ... "vidalsasoon" <kj*****@gmail> wrote in message news:11*********************@c13g2000cwb.googlegro ups... 好的,这是我班级的结构。 " 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

更多推荐

继承问题/困惑

本文发布于:2023-11-17 02:19:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608454.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:困惑

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!