Flash类和MovieClip问题

时间:2010-12-17 10:36:17

标签: flash class movieclip

我有一个奇怪的问题。在Flash CS3 IDE中,我将MovieClip链接到SubSimba类。这个类不扩展MovieClip(它以MovieClip包为基础),而是扩展了SuperSimba(扩展了MovieClip)。怎么了?我在我的Flash项目中实例化SubSimba,如果我在SubSimba构造函数中调用super(),它就像我一样:这里有没有人明白为什么?

package {
    import flash.display.*;
    import flash.events.MouseEvent;

    public class SuperSimba extends MovieClip {
        public function SuperSimba() {
            trace('hi from SuperSimba');
        }
        private function doThis(evt:MouseEvent):void {
            // 
        }
    }
}


package {
    import flash.display.*;
    import flash.events.MouseEvent;

    public class SubSimba extends SuperSimba {
        public function SubSimba() {
            trace('hi from SubSimba');
        }
        private function doThis(evt:MouseEvent):void {
            //
        }
    }
}

2 个答案:

答案 0 :(得分:2)

这是默认行为。

关于为什么要在子类和超级上调用它。这是因为超类在方法的定义中没有虚拟关键字。

在SuperSimba类中,方法doThis()的定义应为:

private virtual function doThis(evt:MouseEvent):void {

这样子类只会被调用。

答案 1 :(得分:1)

我不确定我是否正确理解了这个问题,但默认情况下,默认情况下总是会调用构造函数中的super(),即使您没有明确说明它。所以在SubSimba的构建过程中(无论是在开头还是结尾,我都不确定)将会调用SuperSimba的构造函数。

编辑:

解释我们在评论这个答案时所说的内容。

你必须意识到私人功能不能被覆盖,并且它们的范围仅限于你现在所在的类。因此,如果SuperSimba有doThis()而SubSimba有doThis()这些中的每一个都被认为是单独的范围内的单独函数。

因此,如果在SubSimba中你执行addEventListener(SOME_EVENT, doThis);,它将引用SubSimba的doThis()。它不知道SuperSimba还有doThis函数,因为它是私有的并且超出了范围。

SuperSimba也一样。 SuperSimba中的addEventListener(SOME_EVENT, doThis)实际上引用了在SuperSimba中声明的doThis,因为它不知道在SubSimba中声明的那个(因为它是私有的)。

换句话说,就好像SuperSimba的doThis与SubSimba的命名功能完全不同。私人功能仅适用于其所在的班级,也不适用于其父母和儿童。如果您感到困惑,请阅读有关访问修饰符的内容。

教程:定义构造函数的私有和继承如何激怒你

你有两个班级:

public class Super{
    public function Super(){ //THIS IS CONSTRUCTOR OF SUPER
        addEventListener(SOME_EVENT, doThis); 

        //This references doThis declared in Super
        //It cannot see doThis declared in Sub because it is both private
        //so it isn't overridden so even if you declare doThis in Sub it 
        //is completely different function
    }
    private function doThis(e:*):void{}
}

public class Sub extends Super{
    public function Super(){ //THIS IS CONSTRUCTOR OF SUB
        //There is this funky thing about flash constructors.
        //Event if you DON'T declare super() it will call it
        //without your knowledge. You can't avoid this really,
        //the only you could try is if (false) super() but I am not
        //sure this will work.

        //So, going further, Super's constructor is also called because
        //Sub extends this class. So by adding this event listener here
        //you end up with two listeners for the same event, like:
        //When SOME_EVENT happens call:
        //    doThis declared in Super,
        //    doThis declared in Sub

        addEventListener(SOME_EVENT, doThis)
    }
    private function doThis(e:*):void{}
}

因此,您可以尝试使用if (false) super()或更高级的if (Math.random() > 100) super()来覆盖自动超级调用,但很可能它不起作用。

如果您仍然不明白该问题是什么:http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7fcd.htmlhttp://www.emanueleferonato.com/2009/08/26/undserstanding-as3-access-modifiers-public-internal-protected-private/