Flash Builder中的super().为什么? [英] super() in Flash Builder. WHY?

查看:108
本文介绍了Flash Builder中的super().为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我在Flash Builder中启动一个新的动作脚本类时,它都会从构造函数中开始一行

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?

推荐答案

super()从您正在继承(扩展)的类中调用构造函数.

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

如果继承的(基)类的构造函数中没有必需的参数,则可以将其全部省略,并且Flash会在构造函数代码之前自动调用它.

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.

您可以使用super关键字从基类中调用其他函数(公共的或受保护的):

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

示例:

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");
        }
    }
}

因此,如果您这样做:

var tmp:MyClass = new MyClass();

您将获得:

"My Class constructed"
"Base Class Constructed"

"override"
"some method called from base"

如果省略super(),它将为:

If you omit super(), it will be:

"Base Class Constructed"
"My Class constructed"

"override"
"some method called from base"

这篇关于Flash Builder中的super().为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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