我以前做过,但我不太记得语法了。
我有什么(简化):
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/