繁体   English   中英

python类/子类继承背后的基本原理

[英]rationale behind python class/subclass inheritance

当我创建一个父类和子类,如下所示,为什么父类的参数不会被子类自动拉入?

我明白显性更好,但我想知道这个代码在什么情况下......

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM