当我创建如下所示的父类和子类时,为什么父类的参数没有自动被子类拉入?
我知道显式更好,但我想知道这段代码在什么情况下......
class testParent(object):
def __init__(self,testParentParam1,testParentParam2):
pass
class testChild(testParent):
def __init__(self,testParentParam1,testParentParam2,testChildParam1,testChildParam2):
pass
比这段代码好...
class testParent(object):
def __init__(self,testParentParam1,testParentParam2):
pass
class testChild(testParent):
def __init__(self,testChildParam1,testChildParam2):
pass
最佳答案
派生类扩展基类。这意味着他们在施工时可能需要更多/更少/不同的信息来进行扩展。考虑:
class BaseTextDocument(object):
def __init__(self, content):
self.content = content
class WordDocument(object):
def __init__(self, path, word_version="guess_from_file"):
content = parse_word_document(path, word_version)
super(WordDocument, self).__init__(content)
关于python - python类/子类继承背后的基本原理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15618158/