最好的方法从另一个类在AS3获取变量最好的、变量、方法

2023-09-08 14:59:27 作者:余生浪子

我不知道这其中一种方法是最好的尝试时,得到anotherClass一个变量到主文档级的,为什么?有没有更多的,更好的办法吗?

在例子中,没有1,和主要功能,可以在if-else语句被触发之前checkLogin功能完全做了什么?谢谢!

我没办法1.

 公共类主要扩展影片剪辑{

//声明其他类。
    私人VAR checkLogin:CheckLogin;

    公共函数main(){
        checkLogin =新CheckLogin();
        如果(checkLogin.isLoggedIn ==真){
            跟踪(记录在);
        } 其他 {
            跟踪(未登录);
        }
    }
}
 

 公共类CheckLogin {

    公共变种isLoggedIn:布尔;

    公共职能CheckLogin(){
        isLoggedIn = FALSE;
    }
}
 
如何判断一个策略的好坏

或者是好了很多,做这样一来, (路2号):

 公共类主要扩展影片剪辑{

    //声明其他类。
    私人VAR checkLogin:CheckLogin;

    公共函数main(){
        checkLogin =新CheckLogin();
        checkLogin.addEventListener(checkLogin.go.ready,checkLoginReady);
        checkLogin.go();
    }
    公共职能checkLoginReady(事件= NULL){
        如果(checkLogin.isLoggedIn ==真){
            跟踪(记录在);
        } 其他 {
            跟踪(未登录);
        }
    }
}
 

 公共类CheckLogin扩展EventDispatcher {

    公共变种isLoggedIn:布尔;

    公共职能CheckLogin(){
        isLoggedIn = TRUE;
    }

    公共职能去(){
        this.dispatchEvent(新的事件(checkLogin.go.ready));
    }
}
 

解决方案

一般情况下,使用活动将让你的code更容易维护。

您所描述的登录检查似乎是瞬间,所以方法1号将是确定这样一个简单的例子。

通常的登录检查(或任何处理)可能不是瞬间,例如如果CheckLogin类发送一个服务器请求,等待该响应。在使用事件这种情况下是较好的。主类可以侦听LOGIN_SUCCESS或LOGIN_FAIL事件,当事件发生时处理它。所有登录逻辑保持在CheckLogin。

此外,使用事件将允许多个有兴趣的课才反应过来,没有他们知道任何内部登录过程。

I wonder which one of these methods is the best when trying get a variable from anotherClass to the Main-document-class, and why? Are there any more, even better ways?

In example no 1, and the Main-function, can the if-else statement be triggered before the checkLogin function was completely done? Thanks!

My no 1. way

public class Main extends MovieClip {

// Declaring other classes.
    private var checkLogin:CheckLogin;

    public function Main() {
        checkLogin = new CheckLogin();
        if(checkLogin.isLoggedIn == true) {
            trace("Is logged in");
        } else {
            trace("Not logged in");
        }
    }   
}

and

public class CheckLogin {

    public var isLoggedIn:Boolean;

    public function CheckLogin() {
        isLoggedIn = false;
    }
}

Or is it a lot better to do it this way, (Way no 2):

public class Main extends MovieClip {

    // Declaring other classes.
    private var checkLogin:CheckLogin;

    public function Main() {
        checkLogin = new CheckLogin();
        checkLogin.addEventListener("checkLogin.go.ready", checkLoginReady);
        checkLogin.go();
    }
    public function checkLoginReady(event = null) {
        if(checkLogin.isLoggedIn == true) {
            trace("Is logged in");
        } else {
            trace("Not logged in");
        }
    }
}

and

public class CheckLogin extends EventDispatcher {

    public var isLoggedIn:Boolean;

    public function CheckLogin() {
        isLoggedIn = true;
    }

    public function go() {
        this.dispatchEvent(new Event("checkLogin.go.ready"));
    }
}

解决方案

Generally, using events will make your code easier to maintain.

The login check you describe seems to be instant, so approach number 1 would be ok for such a simple case.

Often the login check (or whatever process) may not be instant, for example if the CheckLogin class sent a server request and waited for the response. In this case using events is better. The Main class can listen for a LOGIN_SUCCESS or LOGIN_FAIL event and handle it when the event occurs. All the login logic stays within CheckLogin.

Also, using events will allow multiple interested classes to react, without them knowing any of the inside login process.