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

标签 python class inheritance subclass

当我创建如下所示的父类和子类时,为什么父类的参数没有自动被子类拉入?

我知道显式更好,但我想知道这段代码在什么情况下......

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/

相关文章:

c# - Entity Framework - 为导航属性指定继承类型的查询

c# - 我可以让我的类在运行时从另一个类继承吗?

python - 为 scrapy 安装 python - 符号链接(symbolic link)和权限问题

java - 访问其他类中的对象

python - 在另一个驱动器上创建 virtualenv - 访问被拒绝

C++ RTTI 继承导致类大小增加

C++ 对象共享错误 LNK2005 和 LNK 1169

仅具有基类实现的 C# 抽象方法?

python - 将 pandas 列名称从蛇形命名法转换为驼峰式命名法

python - 从 Python 调用 MATLAB 脚本时如何调试它