判断题 偏易
#18154

如下列代码所示的基类(base)及其派生类(derived),则生成一个派生类的对象时,只调用派生类的构造函数。 class Base : def __init…

如下列代码所示的基类(base)及其派生类(derived),则生成一个派生类的对象时,只调用派生类的构造函数。

class Base:
	def __init__(self):
		print("Base.__init__ called")
		
class Derived(Base):
	def __init__(self):
		print("Derived.__init__ called")
		super().__init__()
d = Derived()
未登录也能判分,登录后才会记录
答案与解析
正确答案:错
解析

创建一个派生类的对象时,会先调用基类的构造函数,再调用派生类的构造函数。因此,题目中的描述是不正确的。

正确的构造顺序是:

调用基类 base 的构造函数,输出"base constructor"。

调用派生类 derived 的构造函数,输出"derived constructor"。

因此,题目描述 “生成一个派生类的对象时,只调用派生类的构造函数” 是错误的。