Mik*_*son 3 static haxe interface
这个问题已经在旧的Haxe论坛上被问过(并且可能已经回答过)...但看起来整个论坛系统都不再起作用了.因此,我在这里问:
在Haxe中,我需要声明一个包含静态函数"instance()"的类的"接口".但是当我这样做时:
You can't declare static fields in interfaces
所以我删除了"静态"这个词public function instance() [...],我得到了这个:
Field instance needed by [...] is missing.
显然是"Catch-22".但显然必须有一些简单的解决方案.它是什么?
正如您所说,语言不允许接口上的静态字段.选择是有意的.另一件不存在的事情是继承静态字段.
有几种方法可以构建代码以避免这种使用,在我看来它并没有给你带来很多好处.工厂模式或DI方法(我建议minject库)似乎最明显.
鉴于下面的评论,请转到typedef而不是接口:
typedef GetInstance = Void -> Void;
Run Code Online (Sandbox Code Playgroud)
您可以将该typedef作为接口传递给它,其优点是您可以使用static和instance方法来满足该签名.