调用类'方法从另一个类在Flash [英] Calling a Classes' Method From Another Class in Flash

查看:107
本文介绍了调用类'方法从另一个类在Flash的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个文档类:

 包{
    进口的flash.display.MovieClip;

    公共类主要扩展影片剪辑{
        公共函数main(){

        }
        公共职能SomeRandomMethod():无效{

        }
    }
}
 

我怎么能说SomeRandomMethod从这里:

 包{
    公共类AnotherClass {
        公共职能AnotherClass(){

        }
        公共职能AnotherRandomMethod():无效{
            / *我需要在这里使用SomeRandomMethod * /
        }
    }
}
 

解决方案

有几种方法来实现这一目标。一种方法是通过文档类引用其他类的构造函数:

 包{
    公共类AnotherClass {
        私人VAR _doc:主
        公共职能AnotherClass(DOC:主){
            _doc =文档;
        }
        公共职能AnotherRandomMethod():无效{
            _doc.SomeRandomMethod();
        }
    }
}
 

或函数本身

 包{
    公共类AnotherClass {
        公共职能AnotherClass(){

        }
        公共职能AnotherRandomMethod(DOC:主):无效{
            doc.SomeRandomMethod();
        }
    }
}
 

您也可以通过声明一个全局静态变量并赋予该文档类是使用Singleton设计模式。虽然单身被视为反模式。例如:

 包{

        进口的flash.display.MovieClip;

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

            公共静态无功实例:主;

            公共函数main(){
                例如=这一点;
            }
            公共职能SomeRandomMethod():无效{

            }
        }
}
 

然后

 包{
    公共类AnotherClass {
        公共职能AnotherClass(){

        }
        公共职能AnotherRandomMethod():无效{
            Main.instance.AnotherRandomMethod();
        }
    }
}
 

另一种方法是利用服务定位器模式(尽管一些视图它作为抗图案太)。 http://gameprogrammingpatterns.com/service-locator.html

If I have a document class:

package {
    import flash.display.MovieClip;

    public class Main extends MovieClip {
        public function Main() {

        }            
        public function SomeRandomMethod():void {

        }
    }
}

How can I call SomeRandomMethod from here:

package {
    public class AnotherClass {
        public function AnotherClass() {

        }            
        public function AnotherRandomMethod():void {
            /* I need to use SomeRandomMethod here */
        }
    }
}

解决方案

There are a few ways to achieve this. One way would be to pass a reference of the document class to the constructor of the other class:

package {
    public class AnotherClass {
        private var _doc:Main
        public function AnotherClass(doc:Main) {
            _doc = doc;        
        }            
        public function AnotherRandomMethod():void {
            _doc.SomeRandomMethod();
        }
    }
}

or to the function itself

package {
    public class AnotherClass {
        public function AnotherClass() {

        }            
        public function AnotherRandomMethod(doc:Main):void {
            doc.SomeRandomMethod();
        }
    }
}

You could also use a singleton design pattern by declaring a global static variable and assigning the document class to it. Although singletons are regarded as an anti-pattern. For example:

package {

        import flash.display.MovieClip;

        public class Main extends MovieClip {

            public static var instance:Main;

            public function Main() {
                instance = this;
            }            
            public function SomeRandomMethod():void {

            }
        }
}

then

package {
    public class AnotherClass {
        public function AnotherClass() {

        }            
        public function AnotherRandomMethod():void {
            Main.instance.AnotherRandomMethod();
        }
    }
}

Another way would be to make use of the Service Locator pattern (although some view it as an anti-pattern too). http://gameprogrammingpatterns.com/service-locator.html

这篇关于调用类'方法从另一个类在Flash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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