在 ActionScript 3 中的类实例化之前运行代码

发布于 2024-08-24 01:57:36 字数 990 浏览 4 评论 0原文

我需要在类声明实例化之前运行代码。这对于在工厂中自动注册类特别有用。请参阅:

// 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 技术交流群。

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

发布评论

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

评论(2

来世叙缘 2024-08-31 01:57:36

我不能 100% 确定这是否是您想要的,但是您尝试过使用静态初始化程序吗?

public class Foo
{
    // Static Initializer
    {
        Factory.registerClass(CLASS_ID, Foo);
    }

    public static const CLASS_ID : uint = 42;
}

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?

public class Foo
{
    // Static Initializer
    {
        Factory.registerClass(CLASS_ID, Foo);
    }

    public static const CLASS_ID : uint = 42;
}

http://life.neophi.com/danielr/2006/12/static_initializers_in_as3.html

沉睡月亮 2024-08-31 01:57:36

您可以使用编译器选项将类字节代码包含在生成的 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).

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