【发布时间】:2015-06-03 02:32:01
【问题描述】:
我正在尝试基于函数类型参数实例化一个类。
虽然documentation 说有可能,但我做不到。
考虑以下代码:
// Dialog base class
// Every dialog in my application will derive from this
class Dialog
{
public function new()
{
// do some stuff here
}
}
// One of the possible dialogs in the application
// Extends Dialog
class TestDialog extends Dialog
{
public function new()
{
super();
// do some more stuff
}
}
// A simple class that tries to instantiate a specialized dialog, like TestDialog
class SomeAppClass
{
public function new()
{
var instance = create(TestDialog);
}
@:generic
function create<T:Dialog>(type:Class<T>):T
{
return new T();
}
}
这不适用于以下错误:create.T does not have a constructor
很明显,我做错了什么,但是什么?
【问题讨论】:
标签: constructor haxe generic-type-argument