难道我必须调用构造函数中的super()的时候类的ActionScript3扩展雪碧?雪碧、函数、时候、super

由网友(—う小男人无名之卒)分享简介:我总是不叫超()时,我伸出雪碧。但不不调用超()造成什么问题呢?到目前为止,我没有任何问题,我从来没有见过code这在构造函数,类扩展雪碧调用super()。如何文本字段?我没有关于TextField的任何问题了。如何知道我是否应该调用超()或不?I always don't call super() when...

我总是不叫超()时,我伸出雪碧。 但不不调用超()造成什么问题呢? 到目前为止,我没有任何问题,我从来没有见过code这在构造函数,类扩展雪碧调用super()。 如何文本字段? 我没有关于TextField的任何问题了。 如何知道我是否应该调用超()或不?

I always don't call super() when I extends Sprite. But doesn't not calling super() cause any problem? Till now, I don't have any problem and I have never seen code which call super() in constructor which class extends Sprite. How about TextField? I don't have any problem about TextField, too. How to know whether I should call super() or not?

推荐答案

如果闪光灯没有检测到呼叫超()在孩子的构造,然后快速将隐含的通话超()的 的孩子的构造函数之前。所以:

If flash doesn't detect a call to super() in your child constructor then flash will implicitly call super() before your child's constructor. So:

public class Parent {
    public function Parent() {
        trace("Parent");
    }
}

public class Child extends Parent {
    public function Child() {
        trace("Child");
    }
}

new Child();
// Parent
// Child

所以,你的孩子的构造基本上看起来像这样

So your child constructor essentially looks like this

    public function Child() {
        super(); // <-- Added by flash! 
        trace("Child");
    }

所以,不,省略显式调用超()不会的一般的产生不利孩子的班级影响。

So, no, omitting an explicit call to super() will not usually adversely affect your child's class.

那么,为什么要显式调用超()

So why would you want to explicitly call super()?

第一个原因是闪存仅会自动生成一个无参数调用,也就是说,如果你的父类的构造函数的需要的参数,然后你需要使用这些参数显式调用它。如果省略超(参数...)在这种情况下电话,你会得到一个编译错误。

The first reason is flash will only ever automatically generate a parameterless call to super, meaning that if your parent classes constructor requires arguments, then you will need to explicitly call it with those arguments. If you omit the super(args...) call in this case, you will get a compiler error.

二,如果连你的父母有一个参数的构造函数,你可以使用超()来控制秩序的建设者执行。闪光灯将始终插入孩子的构造函数之前调用。所以,如果你想改变这个顺序。然后

Second, if even your parent has a parameter-less constructor, you can use super() to control the order that the constructors execute. Flash will always insert the call before the childs constructor. So if you wanted to change that order. Then

public class Child extends Parent {
    public function Child() {
        trace("Child");
        super()
    }
}

会做相反的顺序。或者,你可以这样做:

would do it in the opposite order. Or you could do:

public class Child extends Parent {
    public function Child() {
        // work before initilizing parent 
        super()
        // work after initilizing parent
    }
}

最后,还有一个很模糊的方式来的没有的说给你父母打电话的构造函数:

Lastly, there is a very obscure way to not call your parents constructor by saying:

public class Child extends Parent {
    public function Child() {
        if(false) super()
    }
}

由于闪存看到有呼叫,它不插入一个。但是,由于其后面的如果(假)它永远不会被调用,所以父类永远不会被初始化。

Because flash sees there is a call, it doesn't insert one. However because its behind a if (false) it never gets called, so the parent class never gets initialized.

阅读全文

相关推荐

最新文章