什么时候需要显式调用超类构造函数? [英] When do you need to explicitly call a superclass constructor?

查看:300
本文介绍了什么时候需要显式调用超类构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以说我有一个子类扩展超类。在什么情况下,我需要显式地键入 super()以获取超类构造函数运行?

So say I have a subclass that extends a superclass. In what scenarios do I need to explicitly type super() to get the superclass constructor to run?

我在一本关于抽象类的书中看到一个例子,当它们用非抽象子类扩展时,子类的默认构造函数是空白的,说超类的默认构造函数将被调用。同时,我也看到了一些实例,在这里有人的问题没有显式调用 super()

I'm looking at an example in a book about abstract classes and when they extend it with a non-abstract subclass, the subclass's default constructor is blank and there's a comment that says the superclass's default constructor will be called. At the same time I've also seen instances on here where someone's problem was not explicitly calling super().

是否区别于从子类的默认/非默认构造函数调用超类的默认/非默认构造函数?

Is the distinction from calling the superclass's default/non-default constructor from the subclass's default/non-default constructor?

推荐答案

从不只需要

super();

如果您没有指定其他内容,那将是什么。您只需需要指定要调用的构造函数:

That's what will be there if you don't specify anything else. You only need to specify the constructor to call if:


  • 您要调用具有参数的超类构造函数

  • 您希望链接到同一类中的另一个构造函数,而不是超类构造函数。

你声称:


同时,我也看到了这里的某些人的问题没有明确调用super()的实例。 / p>

At the same time I've also seen instances on here where someone's problem was not explicitly calling super().

你能给出任何例子吗?我无法想象这是可能的...

Could you give any examples? I can't imagine how that's possible...

这篇关于什么时候需要显式调用超类构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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