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

Cam*_*lis 3 actionscript-3

我以前做过这个,但我不记得语法了.

我得到了什么(简化):

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

我相信这可以在不使用getQualifiedClassName的情况下完成,但它已经很长时间了.任何帮助赞赏.

Jam*_*ett 7

您可能正在将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("");
    }
}

}
Run Code Online (Sandbox Code Playgroud)

这输出:

object
object

object
object

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