在 ActionScript 3 中的类实例化之前运行代码
我需要在类声明实例化之前运行代码。这对于在工厂中自动注册类特别有用。请参阅:
// Main.as
public class Main extends Sprite
{
public function Main() : void
{
var o : Object = Factory.make(42);
}
}
// Factory.as
public class Factory
{
private static var _factory : Array = new Array();
public static function registerClass(id : uint, c : Class) : void
{
_factory[id] = function () : Object { return new c(); };
}
public static function make(id : uint) : Object
{
return _factory[id]();
}
}
// Foo.as
public class Foo
{
// Run this code before instanciating Foo!
Factory.registerClass(CLASS_ID, Foo);
public static const CLASS_ID : uint = 42;
}
据我所知,ActionScript 语言的 JIT 机器不允许我这样做,因为在 Main
方法中没有引用 Foo
。正在生成 Foo
类,我无法(也不想)在 Main
中注册这些类:我想在 a 中注册所有导出的类特定的包(或库)。理想情况下,这可以通过包内省来完成,而 ActionScript 3 中不存在这种功能。
您知道我的设计问题有任何修复(或其他解决方案)吗?
I need to run code in a class declaration before its instanciation. This would be especially useful to automatically register classes in a factory. See:
// Main.as
public class Main extends Sprite
{
public function Main() : void
{
var o : Object = Factory.make(42);
}
}
// Factory.as
public class Factory
{
private static var _factory : Array = new Array();
public static function registerClass(id : uint, c : Class) : void
{
_factory[id] = function () : Object { return new c(); };
}
public static function make(id : uint) : Object
{
return _factory[id]();
}
}
// Foo.as
public class Foo
{
// Run this code before instanciating Foo!
Factory.registerClass(CLASS_ID, Foo);
public static const CLASS_ID : uint = 42;
}
AFAIK, the JIT machine for the ActionScript language won't let me do that since no reference to Foo
is made in the Main
method. The Foo
class being generated, I can't (and don't want to) register the classes in Main
: I'd like to register all the exported classes in a specific package (or library). Ideally, this would be done through package introspection, which doesn't exist in ActionScript 3.
Do you know any fix (or other solution) to my design issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不能 100% 确定这是否是您想要的,但是您尝试过使用静态初始化程序吗?
http://life.neophi.com/danielr/2006/12/static_initializers_in_as3。 html
I'm not 100% sure sure if this is what you're after, but have you tried using a Static Initializer?
http://life.neophi.com/danielr/2006/12/static_initializers_in_as3.html
您可以使用编译器选项将类字节代码包含在生成的 SWF 或 SWC 中。但您必须使用 MXMLC(或用于 SWC 的 COMPC)进行编译。
You can use compiler options to include class byte code in the resulting SWF or SWC. But you have to compile with MXMLC (or COMPC for SWCs).