我已经在我的flex项目“B”,“C”和“D”中定义了3个接口。 “D”接口扩展“B”接口,“C”接口是“B”型实例的使用者。之后,我定义了2个模块,M1和M2。 M1实现“D”接口,M2实现“C”接口。 M2的公共职能如下。通过接口在Flex模块之间进行通信
/* 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
}
}
然后我定义2台模块装载机(mld1 & mld2)加载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
然而,当它调用M2.consume(B):在第3行void函数,“如果”判定在消耗功能(线1)总是会失败和“如果”的体结构总是会被跳过。但如果我在第3行之前将“M2”模块中第1行所示的类型确定行添加到主应用程序,则第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”和“C”接口。
谢谢!
没有什么个人的,但我简单的头脑被所有这些字母汤困惑:)我认为如果你显示变量名有意义,你的解释会更清楚。去给它另一个读过... –
@Sunil D.谢谢你的建议。我非常感激。事实上,我试图用一种有意义的方式命名它们,“B”表示Base接口,“C”表示Consumer接口,“D”表示Derived接口。他们只是把第一个字母作为他们的ID。那么,也许我没有明确指出这一点。 – xnslong