热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

可以使用类型引用声明AS3向量吗?-CanAS3vectorsbedeclaredwithatypereference?

Insteadofthis:而不是这个:varv:Vector.<String>newVector.<String>();isthereanyw

Instead of this:

而不是这个:

var v:Vector. = new Vector.();

is there any way to do something like this?

有没有办法做这样的事情?

var myType:Class = String;
var v:Vector. = new Vector.();

Obviously that doesn't work as written, but hopefully you get the idea.

显然这不符合书面形式,但希望你能得到这个想法。

3 个解决方案

#1


Short answer is try grapefrukt's answer and see.

简短的回答是尝试grapefrukt的答案,看看。

However, I don't think it's possible at a bytecode level. The problem related to how generics (Vectors) are constructed. Basically the bytecode for creating an instance of Vector<> goes:

但是,我不认为它在字节码级别是可能的。问题与如何构造泛型(Vectors)有关。基本上,用于创建Vector <>实例的字节码是:

GenericDefinitionType (Vector) + GenericParameter (int) -> GenericType
Coerce (cast) GenericType as KnownGenericType (eg. "Vector.")

So the issue is not in the creation, since GenericParameter is just a multiname (which can be dynamic). The issue is in the coercion to the known vector type (actually registered as "Vector." for example) since there is no known vector type.

所以问题不在于创建,因为GenericParameter只是一个多重名称(可以是动态的)。问题在于对已知矢量类型的强制(例如,实际上注册为“Vector。 ”),因为没有已知的矢量类型。

See my post on how Vectors work in bytecode for the geeky details.

有关令人讨厌的详细信息,请参阅我关于Vector如何在字节码中工作的帖子。

#2


I found sort of a way to create Vectors dynamically. Instead of passing the type as class you pass the whole vector as class, like:

我找到了一种动态创建向量的方法。不要将类型作为类传递,而是将整个向量作为类传递,如:

public function createVector (vectorType:Object):Object
{
    return new vectorType();
}

var v:Vector. = createVector(Vector.);

Or you can copy a vector like this:

或者您可以复制这样的矢量:

public function getCopy (ofVector:Object):Object
{
    var copy:Object = new ofVector.constructor;

    return copy;
}

#3


This is untested, but I can't see why it shouldn't work:

这是未经测试的,但我不明白为什么它不应该工作:

import flash.display.Sprite;
import flash.utils.getDefinitionByName;

var ClassReference:Class = getDefinitionByName("flash.display.Sprite") as Class;
var v:Vector. = new Vector.();

推荐阅读
author-avatar
莪系坏女孩2010
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有