传递类作为参数,然后从类实例化 [英] pass class as parameter, then instantiate from class

查看:116
本文介绍了传递类作为参数,然后从类实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've done this before, but i can't quite remember the syntax.

我的本钱(简体):

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

我相信这可以在不使用的getQualifiedClassName来完成,但它是一个很长的时间。任何帮助AP preciated。

I believe this can be done without using getQualifiedClassName, but its been a long time. Any help appreciated.

推荐答案

您可能正在通过空的功能。

You are probably passing null to the function.

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()

这篇关于传递类作为参数,然后从类实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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