actionscript-3 - 将类作为参数传递,然后从类实例化

标签 actionscript-3

我以前做过,但我不太记得语法了。

我有什么(简化):

function createText(clazz:Class)
{
    var font:Font = new clazz(); //throws Instantiation attempted on a non-constructor.
}

我相信这可以在不使用 getQualifiedClassName 的情况下完成,但已经很久了。任何帮助表示赞赏。

最佳答案

您可能将 null 传递给函数。

package
{

import flash.display.Sprite;

public class ClassTest extends Sprite
{
    function ClassTest()
    {
        makeObject(Object);
        makeObject(Sprite);
        makeObject(null);
    }

    private function makeObject(type:Class):void
    {
        trace(typeof type);
        var obj:* = new type();
        trace(typeof obj);
        trace("");
    }
}

}

这个输出:

object
object

object
object

object
TypeError: Error #1007: Instantiation attempted on a non-constructor.
    at ClassTest/makeObject()
    at ClassTest()

关于actionscript-3 - 将类作为参数传递,然后从类实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1211181/

相关文章:

actionscript-3 - Apache Royale 中的 id 和 localId 有什么区别?

android - Flash 中的舞台定位

apache-flex - 在多次调用的函数中增加全局变量

actionscript-3 - AS3 - 又一个 TypeError : Error #1009 Cannot acces

mysql - 如何在as3中创建随机名称生成器

flash - 如何在 AS3 中为文本添加描边?

actionscript-3 - 所有 child 均已禁用,只有一只鼠标处于事件状态

actionscript-3 - 使用as3创建正弦波音频-上下扫频

c++ - 通过 C++ 为 AIR native 扩展生成可变长度的字节数组

actionscript-3 - 弹性 : Access static constant from parent