Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Java中是否必须调用基类构造函数?在 C++ 中它是可选的,所以我问这个。
当我扩展ArrayAdapter时,我收到此错误:"Implicit super constructor ArrayAdapter<String>() is undefined. Must explicitly invoke another constructor"
ArrayAdapter
"Implicit super constructor ArrayAdapter<String>() is undefined. Must explicitly invoke another constructor"
那么,调用基构造函数的目的是什么?当我创建对象基类构造函数将调用 & 然后它涉及派生权。
如果您自己不调用无参数构造函数,则会隐式调用该构造函数,如果该构造函数不存在,则该构造函数无效。需要调用超构造函数的原因是超类通常具有一些在构造后期望处于的状态,其中可能包括无法在子类中设置的私有变量。如果不调用构造函数,它会使对象处于可能无效的状态,这可能会导致各种问题。
不需要调用超类的无参数构造函数。如果你想用 args 调用构造函数,用户super关键字如下所示:
super(arg1, ...);