简体   繁体   English

通过另一个类访问构造函数定义的变量

[英]Accessing a variable defined by a constructor function through another class

I have two classes, the first one is called CoreModus. 我有两个类,第一个叫CoreModus。 In CoreModus (which is a non-document class) I declare a "global" variable, called modus, using the constructor function CoreModus(modus); 在CoreModus(这是一个非文档类)中,我使用构造函数CoreModus(modus);声明了一个名为modus的“全局”变量CoreModus(modus);

CoreModus.as: CoreModus.as:

package myStudio.basic {
    public class CoreModus {
        public var modus:String;
        public function CoreModus(structure:String) {
            modus = structure;
        }
        public function setup():String {
            return modus;
        }
    }
}

The second class is called Animation, which is a non-document class, and I want to access the variable modus, which is declared in CoreModus constructor function: ie CoreModus("non-linear"); 第二个类称为动画,它是一个非文档类,我想访问变量modus,该变量在CoreModus构造函数中声明:ie CoreModus(“ non-linear”); which in this case modus = non-linear . 在这种情况下, modus = non-linear

Animation.as: Animation.as:

package myStudio.basic {
    import fl.transitions.Tween
    public class Animation {
        public var anim:Tween;
        public function Animation() {}
        public function tryToRetrieveModus():void {
            var modo:CoreModus = new CoreModus();
            var modus:String = modo.getModus();
            trace("I'm trying to retrieve the modus " + modus);
        }
    }   
}

Of course, because CoreModus needs a parameter, I can't use the option I tried in Animation.as (making an instance of CoreModus). 当然,因为CoreModus需要一个参数,所以我不能使用我在Animation.as(创建CoreModus的实例)中尝试​​的选项。

FLA document, frame 1: FLA文件,框架1:

import myStudio.basic.CoreModus;
import myStudio.basic.Animation;

var modo:CoreModus = new CoreModus("non-linear");
var mov1:Animation = new Animation();

trace(modo.setup());
mov1.tryToRetrieveModus();

Is there any other way to access to this variable? 还有其他方法可以访问此变量吗?

PS I omitted a bunch of unrelated lines in CoreModus(); PS我在CoreModus()中省略了一堆无关的代码; constructor function. 构造函数。 I don't want that code to be processed every time, for CPU's sake. 为了CPU的缘故,我不希望每次都处理该代码。

To access the variable from your CoreModus class instance, first create the instance of the class somewhere. 要从您的CoreModus类实例访问变量,请首先在某个地方创建该类的实例。

Then, pass the instance reference to the the Animation class instance. 然后,将实例引用传递给Animation类实例。 After that you can use the public variable of CoreModus as you please. 之后,您可以根据需要使用CoreModus的公共变量。

Here's an example: 这是一个例子:

//creating the CoreModus class instance
var myCoreModus:CoreModus = new CoreModus("my string");

//creating the Animation class instance
var myAni:Animation = new Animation();
myAni.modo = myCoreModus; // make sure that modo is public instance variable

You can also make your CoreModus a singleton and have a static variable if you have only one instance of your CoreModus. 如果您只有CoreModus的一个实例,也可以使CoreModus为单例并具有静态变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM