Java中的默认构造函数和继承 [英] Default constructors and inheritance in Java
问题描述
我对Java中的默认构造函数和继承有疑问。
I have a question about default constructors and inheritance in Java.
通常,如果你编写一个类并且不包含任何构造函数,Java会自动为你提供一个默认构造函数(一个没有参数),它初始化所有的实例变量。 class(如果有)具有一些默认值(0,null或false)。但是,如果使用某些参数编写构造函数,并且不编写任何默认构造函数,则Java不提供默认构造函数。
我的问题是:类是什么情况,它继承自其他类 - 如果我编写一个带有一些参数的构造函数,但是不包含默认构造函数,它们是否继承了超级默认构造函数class?
Generally, if you write a class and do not include any constructor, Java provides automatically for you a default constructor (one without parameters), which initializes all instance variables of the class (if there are any) with some default values (0, null, or false). If you write a constructor, however, with some parameters, and you don't write any default constructor, then Java does not provide a default constructor. My question is: what is the case with classes, which inherit from other classes - if I write a constructor with some parameters in them, but don't include a default constructor, do they inherit the default constructor of the super class?
推荐答案
-
如果你没有构造函数,自动创建默认的空构造函数。
如果任何构造函数没有显式调用超级或此构造函数作为其第一个语句,自动添加对super()的调用。
If any constructor does not explicitly call a super or this constructor as its first statement, a call to super() is automatically added.
始终。
这篇关于Java中的默认构造函数和继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!