首先需要注意的是构造函数。每个类都有构造函数,继承类在生成对象的时候,是不会自动调用父类的构造函数的,
因此你必须在 init() 函数中显式调用父类的构造函数。它们的执行顺序是 子类的构造函数 -> 父类的构造函数。
class Entity():
def __init__(self, object_type):
print('parent class init called')
self.object_type = object_type
class Document(Entity):
def __init__(self, title, author, context):
print('Document class init called')
Entity.__init__(self, 'document')
a=Document('aa','bb','cc')
print a.object_type
C:\Python27\python.exe "C:/Users/TLCB/PycharmProjects/untitled2/deep study/t3.py"
Document class init called
parent class init called
document
Process finished with exit code 0