super() in Flash Builder. WHY?

孤街浪徒 提交于 2020-01-15 06:58:26

问题


Every time I start a new actionscript class in Flash Builder it starts off the constructor with a line

super()

I have never seen this before, and it seems to have no purpose. Deleting it results in the exact same movie.

Why is it inserted into my new class and what does it do?


回答1:


super() calls the constructor from the class that you're inheriting (extending).

If your inherited (base) class has no required parameters in it's constructor, you can omit it all together and flash will automatically call it before your constructor code.

You can call other functions (that are public or protected) from your base class by using the super keyword:

super.myBaseClassMethod(); //would call the method 'myBaseClassMethod' from your base class even if you had an overriden method with in this class

EXAMPLE:

package {
    public class BaseClass {
        public function BaseClass(){
            trace("Base Class Constructed");
        }

        public function someBaseMethod():void {
            trace("some method called from base");
        }
    }

}

package {
    public class MyClass extends BaseClass {  //this class is extending the class above
        public function MyClass():void {
            trace("My Class constructed");

            super();
            someBaseMethod();
            super.someBaseMethod();
        }

        override public function someBaseMethod():void {
            trace("Override");
        }
    }
}

So if you do this:

var tmp:MyClass = new MyClass();

You will get:

"My Class constructed"
"Base Class Constructed"

"override"
"some method called from base"

If you omit super(), it will be:

"Base Class Constructed"
"My Class constructed"

"override"
"some method called from base"



回答2:


As a part of inheritance, super invokes the superclass or parent version of a method or constructor.

Invokes the superclass or parent version of a method or constructor. When used within the body of a class constructor, the super() statement invokes the superclass version of the constructor. The call to the superclass constructor must have the correct number of arguments. Note that the superclass constructor is always called, whether or not you call it explicitly. If you do not explicitly call it, a call with no arguments is automatically inserted before the first statement in the subclass constructor body. This means that if you define a constructor function in a subclass, and the superclass constructor takes one or more arguments, you must explicitly call the superclass constructor with the correct number of arguments or an error will occur. The call to the superclass constructor, however, does not need to be the first statement in your subclass constructor, as was required in ActionScript 2.0.

When used in the body of an instance method, super can be used with the dot (.) operator to invoke the superclass version of a method and can optionally pass arguments (arg1 ... argN) to the superclass method. This is useful for creating subclass methods that not only add additional behavior to superclass methods, but also invoke the superclass methods to perform their original behavior.

You cannot use the super statement in a static method.

In ActionScript, classes can extend other base classes not marked as final.

For example, MovieClip inheritance is as follows:

Sprite > DisplayObjectContainer > InteractiveObject > DisplayObject > EventDispatcher > Object

By invoking super(), you control when parent constructors are called.

package
{
    import flash.display.MovieClip;

    public class ExampleMovieClip extends MovieClip
    {
        public function ExampleMovieClip()
        {
            super(); // MovieClip's constructor is called
        }
    }
}


来源:https://stackoverflow.com/questions/16615831/super-in-flash-builder-why

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!