如何在AS3中动态创建向量?

Mar*_*rty 3 vector actionscript-3

我想创建一个主要包含a的类Vector.该类将有一些方法来处理Vector中的项目.

我目前遇到的问题是我无法弄清楚如何动态创建一个实例Vector.到目前为止,我已经尝试了这个和类似的没有运气:

public class List
{
    private var _content:Vector;

    public function List(type:Class)
    {
        _content = new Vector.<type>();
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 6

这是我如何动态构造BitmapData的向量(MouseCursorData类所需):

var vectorClassOfBitmapData:Class = Class(getDefinitionByName("__AS3__.vec::Vector.<flash.display::BitmapData>"));

var bitmapDataVector:* = new vectorClassOfBitmapData(1,true);
Run Code Online (Sandbox Code Playgroud)

以上与编译时相同:

var bitmapDataVector:* = new Vector.<BitmapData>(1, true);
Run Code Online (Sandbox Code Playgroud)

通过这种方式,您可以在运行时组合类定义字符串,并使用getDefinitionByName动态构造不同数据类型的向量.

不完全是你所追求的,但它可能会帮助别人.


And*_*dri 5

Paul Robertson(以前是Adobe的高级ActionScript开发人员/编写者)发表的这篇文章提供了有关如何声明Vectors的更多信息:

Vector类允许(要求)指定它在编译时将包含的类型 - 包括变量声明和创建实例时的类型.

因为type参数是文字,所以必须在编译时提供.实际上,在编译时会检查对Vector的每个引用,但运行时会检查.shift()和除外.unshift.

Adobe关于索引数组的文章提供了一些有趣的信息.实际上,它提到严格的编译时类型安全性是Vector的关键特性之一.

简而言之:不可能使用变量来设置Vector的类型,因为type参数是文字和编译时间要求.

希望有所帮助!

其他参考文献: