从 Haxe 中的字符串名称创建类的实例

发布于 2024-09-18 04:50:13 字数 91 浏览 6 评论 0原文

假设我获取了一个以 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扬花落满肩 2024-09-25 04:50:13
var instance : MyClass = Type.createInstance(Type.resolveClass("path.to.MyClass"), []);

几点注意事项:

  • resolveClass() 采用您需要的类的完整路径(包括包)
  • createInstance() 将应用到的值的数组作为第二个参数构造函数。这些值必须是精确的数字,并且即使它们是可选的也必须传递(在这种情况下,空值很好)。
var instance : MyClass = Type.createInstance(Type.resolveClass("path.to.MyClass"), []);

Few notes:

  • 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).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文