flex模块之间通过接口进行通信



我在我的flex项目中定义了3个接口,"B", "C"one_answers"D"。"D"接口扩展了"B"接口,"C"接口是"B"类型实例的消费者。之后,我定义了两个模块,M1和M2。M1实现"D"接口,M2实现"C"接口。M2的公共功能如下。

/* in the "M2" module */
// the stub is declared in the "C" interface.
public function consume(b:B):void{ 
    if(b is D){                  // line 1: type determination
        // do something on the D interface
    }
}

然后我定义了两个模块加载器(mld1 &在主应用程序中加载M1和M2(通过设置url)。在M1和M2都加载后,我试图通过在M2模块中实现的"C.consume(B):void"函数为M2提供M1。代码如下所示。

/* in the "main" application */
var m1:B = mld1.child as B;      // line 2: cast type to B
var m2:C = mld2.child as C;
m2.consume(m1);                  // line 3: provide m1 instance for m2

然而,当它在第3行调用M2.consume(B):void函数时,consume函数(第1行)中的"if"判断总是失败,并且"if"结构体总是被跳过。但是,如果我将"M2"模块中第1行所示的类型确定行添加到第3行之前的主应用程序中,那么第1行中的类型确定将成功。也就是说,主应用程序中的以下代码将使传递第1行中的类型确定成为可能。

 /* in the "main" application: make type determination be line 3 */
var m1:B = mld1.child as B;     // line 2: cast type to B
if(m1 is D)                     // just make a plain determination. nothing else.
    ;                            
var m2:C = mld2.child as C;
m2.consume(m1);                 // line 3: provide m1 instance for m2

或者直接将类型强制转换为D类型,也会得到相同的结果。

/* in the "main" application: make type cast before line 3 */
var m1:B = mld1.child as D;     // line 2: after modified, cast type to D.
var m2:C = mld2.child as C;
m2.consume(m1);                 // line 3: provide m1 instance for m2

我只是想知道为什么只有当我在主应用程序中提到"D"类型时,第1行中的确定才会成功。主应用程序中的类型确定或类型强制转换会对目标对象产生任何影响吗?如果我希望主应用程序只知道"B"接口和它的用户接口("C"接口),那么应用程序可以支持"B"one_answers"C"接口的任何子接口和类,我该怎么做?

谢谢!

很难理解您所写的所有内容,所以可能我错过了一些东西,但是如果您不将D导入到主应用程序中,它将不会被编译成最终的SWF。这就是为什么主应用程序不会意识到d

最新更新