什么是Java上的隐式构造函数 [英] What is Implicit constructors on Java
问题描述
是否必须在Java中调用基类构造函数?
在C ++中是可选的,所以我问这个。
Is it mandatory to call base class constructor in Java? In C++ it was optional, so I am asking this.
当我扩展 ArrayAdapter
得到这个错误:隐式超级构造函数ArrayAdapter< String>()未定义必须显式调用另一个构造函数
When I extend ArrayAdapter
, I get this error: "Implicit super constructor ArrayAdapter<String>() is undefined. Must explicitly invoke another constructor"
那么,调用基础构造函数的目的是什么?当我创建对象时,基类构造函数将调用&
So, what is the purpose of calling base constructor? When I create object base class constructor will call & then it comes to derived right.
推荐答案
如果你不自己调用,那么无参数构造函数会被隐式调用如果该构造函数不存在则无效。调用超级构造函数的原因是,超类通常具有一些在构造之后期望处于的状态,这可能包括不能在子类中设置的私有变量。如果不调用构造函数,它将使对象处于可能无效的状态,这可能导致各种问题。
The no-args constructor is called implicitly if you don't call one yourself, which is invalid if that constructor doesn't exist. The reason it is required to call a super constructor is that the superclass usually has some state it expects to be in after being constructed, which may include private variables that can't be set in a sub-class. If you don't call the constructor, it would leave the object in a probably invalid state, which can cause all kinds of problems.
这篇关于什么是Java上的隐式构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!