当类在actionscript3中扩展Sprite时,我不需要在构造函数中调用super()吗?
当我扩展 Sprite 时,我总是不调用 super()。
但是不调用 super()
不会导致任何问题吗?
到目前为止,我没有任何问题,而且我从未见过在类扩展 Sprite 的构造函数中调用 super() 的代码。
文本字段怎么样?
我对 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果 flash 在子构造函数中没有检测到对
super()
的调用,那么 flash 将隐式调用super()
之前< /em> 你孩子的构造函数。所以:所以你的子构造函数本质上是这样的
所以,不,省略对 super() 的显式调用不会通常对你孩子的类产生不利影响。
那么为什么要显式调用
super()
呢?第一个原因是 flash 只会自动生成对 super 的无参数调用,这意味着如果您的父类构造函数需要参数,那么您将需要使用这些参数显式调用它论据。如果在这种情况下省略 super(args...) 调用,您将收到编译器错误。
其次,如果您的父级也有无参数构造函数,您可以使用 super() 来控制构造函数的执行顺序。 Flash 始终会在子构造函数之前插入调用。所以如果你想改变这个顺序。然后
会按照相反的顺序进行。或者您可以这样做:
最后,有一种非常晦涩的方法可以不调用您的父构造函数,方法是:
因为 flash 看到有一个调用,所以它不会插入一个调用。然而,因为它位于
if (false)
后面,所以它永远不会被调用,因此父类永远不会被初始化。If flash doesn't detect a call to
super()
in your child constructor then flash will implicitly callsuper()
before your child's constructor. So:So your child constructor essentially looks like this
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 thesuper(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. Thenwould do it in the opposite order. Or you could do:
Lastly, there is a very obscure way to not call your parents constructor by saying:
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.如果您没有显式调用
super()
,Flash 会在构造函数中的所有其他代码之前自动执行此操作。如果您显式调用
super()
,它将在您编写它的行处调用。但请注意,在实例化超类之前,您无法设置或获取任何
this
或super
属性或调用任何方法If you don't call
super()
explicitly, Flash will do it automatically before all other code in your constructor.If you call
super()
explicitly, it will be called at the line on which you wrote it.However, note that you can not set or get any
this
orsuper
properties or call any methods before the super class is instantiated您可以安全地排除对基本构造函数的调用。如果您不在构造函数中调用 super(),编译器将添加对不带参数的基本构造函数的调用。
You can safetly exclude the call to the base constructor. If you do not call
super()
in the constructor, the compiler will add a call to the base constructor with no arguments.