簡體   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