如何从另一个类访问公共数组/向量 [英] How to access a public array/vector from another class

查看:136
本文介绍了如何从另一个类访问公共数组/向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Main class文件中有一个用于存储对象的向量.我希望能够从其他类向该SAME向量添加更多对象.我在主类中为矢量赋予了"public"修饰符.我现在需要在其他类文件中引用它的语法

I have a vector in my Main class file that store objects. I will like to be able to add more objects to that SAME vector from a different class. I gave the vector in my main class the "public" modifier. I now need the syntax to reference it in my other class file

public var badChar:Vector.;

public var badChar:Vector.;

推荐答案

您可以选择.处理方法的方式取决于您的项目设置以及属性的需求.它是一个实例化的对象,还是应该只有一个(即使该类被实例化了多次)?您是否需要直接访问它,而与舞台没有任何关系?下面的每个解决方案都有其优缺点.

You have options. How you approach it is dependent on your project setup, and the needs of the property. Is it an instantiated object, or should there ever only be one (even if the class is instantiated multiple times)? Do you need direct access to it regardless of any relationship to the stage? Each solution below has pros and cons.

假定以下主要foo.as类:

package {
    public class Foo {
        public var bool:Boolean = true;
    }
}

酒吧班:

package {
    public class Bar extends Sprite {
        import flash.events.Event;

        public function Bar() {
            addEventListener(Event.ADDED_TO_STAGE, accessFoo);
        }

        private function accessFoo(e:Event):void {
            trace(this.parent["f"].bool); // traces "true"
        }
    }
}

文档代码:

var f:Foo = new Foo();
var b:Bar = new Bar();
addChild(b);


继承

Foo类:


Inheritance

Foo Class:

package {
    public class Foo {
        public var bool:Boolean = true;
    }
}

酒吧班

package {
    public class Bar extends Foo {
        public function Bar() {
            trace(bool); // traces "true"
        }
    }
}


通过静态的类到类

应该使用某些免责声明来使用Static属性,但我将让您继续阅读这些内容.


Class-to-Class via Static

Some disclaimers should be in order for Static properties, but I'll leave you to read up on those.

Foo类:

package {
    public class Foo {
        public static var bool:Boolean = true;
    }
}

酒吧班

package {
    public class Bar {
        public function Bar() {
            trace(Foo.bool); // traces "true"
        }
    }
}


通过新声明直接访问

Foo类:


Direct Access via New Declaration

Foo Class:

package {
    public class Foo {
        public var bool:Boolean = true;
    }
}

酒吧班

package {
    public class Bar {
        import Foo;

        public function Bar() {
            trace(new Foo().bool); // traces "true"
        }
    }
}


通过共享访问

Foo类:


Access via Sharing

Foo Class:

package {
    public class Foo {
        public var bool:Boolean = true;
    }
}

酒吧课程

package {
    public class Bar {
        import Foo;
        public var fluffy:Foo;

        public function Bar() {
            trace(fluffy.bool);
        }
    }
}

文档代码:

var f:Foo = new Foo();
var b:Bar = new Bar();
b.fluffy = f;

请注意,在文档代码的第三行之后,fluffy不再是未声明的变量,现在将指向f对象,其中更新的属性(例如bool)将在其中反映出来. Bar.

Note that after the third line in the document code, fluffy is no longer an undeclared variable and will now point to the f object, where the properties updated in it (such as bool) will reflect inside of Bar.

这篇关于如何从另一个类访问公共数组/向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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