静态主类 - AS3 [英] Static Main class - AS3

查看:132
本文介绍了静态主类 - AS3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法,使主类 - 基于主.fla文件之一 - 静态?所以我们可以把它作为在Java中,能够从其他类中引用它,因为我必须通过主本身作为参数传递给一个类的实例,否则我失去参考。 我尝试添加静态preFIX,但似乎AS3不允许它。

Is there a way to make the main class - the one based on the main .fla - static? so we could use it as in java, being able to reference it from other classes, because I have to pass the instance of the main itself as a parameter to a class, otherwise I loose reference. I tried to add static prefix but seems as3 doesn't allow it.

推荐答案

没有静态类在AS3的理念。

There is not concept of static class in AS3.

您可以使用Singleton模式暴露你的主类的唯一实例:

You can use the singleton pattern to expose the unique instance of your main class :

public class Main extends Sprite {

    public static var instance:Main;

    public function Main() {
         instance = this;
    }

...或者只是标记公开其他属性/方法,如静态,虽然它变得更难管理。

...or just mark expose other properties/methods as static, although it becomes harder to manage.

我要提的,虽然,使用静态有时会导致不好的code设计(而不是,你可以通过的实例主需要它通过其构造函数实例类)

I need to mention though that using static sometimes leads to bad code design (instead, you can pass the instance of Main to classes that need it through their constructor for instance)

这篇关于静态主类 - AS3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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