当我创建父类和子类时,如下所示,为什么父类的参数不会被子类自动拉入?
我明白显性更好,但我想知道这个代码在什么情况下......
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
答案 0 :(得分:4)
派生类扩展基类。这意味着他们可能在施工时需要更多/更少/不同的信息来进行扩展。考虑:
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)