从Haxe中的字符串名称创建类的实例 [英] Create an instance of a class from a string name in Haxe

查看:106
本文介绍了从Haxe中的字符串名称创建类的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我获得了我作为String做的一类的名称.如何使用该字符串中包含的名称实例化该类?我知道它将从某个父类派生,但实际的类会有所不同.

Let's say i acquire the name of a class that i made as a String. How can i Instantiate the class with the name contained in that string? I I know it will be derived from a certain parent class, but the actual class will vary.

推荐答案

var instance : MyClass = Type.createInstance(Type.resolveClass("path.to.MyClass"), []);

一些注意事项:

  • resolveClass()包含所需类的完整路径(包括软件包)
  • createInstance()将第二个参数作为应用于构造函数的值的数组.这些值必须是准确的数字,并且即使它们是可选的也必须传递(在这种情况下,null很好).
  • resolveClass() takes the full path (packages included) of the classe you need
  • createInstance() takes as the second argument an array of values that are applied to the constructor. Those values must be in the exact number and must be passed even if they are optional (nulls are good in that case).

这篇关于从Haxe中的字符串名称创建类的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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