直接上结论:Python中子类会自动调用父类的初始化函数如果子类没有初始化函数,详情可以看以下代码。
class son(Parent):
pass
class Parent():
def __init__(self):
print("go goes function of father's initialization here")
def make_sound(self):
print("The animal makes a sound.")
second_son=son()
second_son.make_sound()
底下是输出:
go goes function of father's initialization here
The animal makes a sound.
底下是反例:
class son(Parent):
def __init__(self):
print("go goes function of son's initialization here")
class Parent():
def __init__(self):
print("go goes function of father's initialization here")
def make_sound(self):
print("The animal makes a sound.")
second_son=son()
second_son.make_sound()
以下是输出:
go goes function of son's initialization here
The animal makes a sound.