当类在 actionscript3 中扩展 Sprite 时,我不必在构造函数中调用 super() 吗? [英] Don't I have to call super() in constructor when class extends Sprite in actionscript3?

查看:16
本文介绍了当类在 actionscript3 中扩展 Sprite 时,我不必在构造函数中调用 super() 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我扩展 Sprite 时,我总是不调用 super().
但是不调用 super() 不会导致任何问题吗?

到目前为止,我没有任何问题,而且我从未见过在构造函数中调用 super() 的代码,该类扩展了 Sprite.

TextField 怎么样?
我对 TextField 也没有任何问题.

如何知道我是否应该调用 super() ?

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?

推荐答案

如果 flash 在子构造函数中没有检测到对 super() 的调用,则 flash 将隐式 调用 super() before 你孩子的构造函数.所以:

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

所以,不,省略对 super() 的显式调用不会通常对您孩子的班级产生不利影响.

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

那你为什么要显式调用super()?

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

第一个原因是 flash 只会自动生成对 super 的无参数调用,这意味着如果您的父类构造函数 需要 参数,那么您将需要显式调用它与这些论点.如果在这种情况下省略 super(args...) 调用,则会出现编译器错误.

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.

第二,即使你的父级也有一个无参数的构造函数,你可以使用 super() 来控制构造函数的执行顺序.Flash 将始终在 childs 构造函数之前插入调用.因此,如果您想更改该顺序.那么

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()
    }
}

因为 flash 看到有一个调用,它不会插入一个.然而,因为它在 if (false) 后面,它永远不会被调用,所以父类永远不会被初始化.

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.

这篇关于当类在 actionscript3 中扩展 Sprite 时,我不必在构造函数中调用 super() 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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