相关疑难解决方法(0)

python继承中父类和子类可以相同吗?

在python继承中,我们通常可以将父类的属性继承给子类。但是,我不明白在同一个类中继承的想法。这是什么意思?

class MyParentClass():
    def __init__(self):
        super(MyParentClass, self).__init__()
Run Code Online (Sandbox Code Playgroud)

python oop inheritance class

1
推荐指数
1
解决办法
650
查看次数

如何让一个类在 Python 中继承其父级的值?

这是我想要做的事情的粗略想法:

class PlayerModel(Timeline):
    def __init__(self, name):
        self.name = name

        self.timeline_min = super(timeline_min)     # I don't know the syntax for this
        self.timeline_max = super(timeline_max)     # I don't know the syntax for this


class Timeline:
    def __init__(self):
        self.timeline_min = 0
        self.timeline_max = 120

    def make_model(self, m_name):
        return PlayerModel(m_name)
Run Code Online (Sandbox Code Playgroud)

我想PlayerModel拥有相同的属性Timeline

self.timeline_min = 0
self.timeline_max = 120
Run Code Online (Sandbox Code Playgroud)

在 it's 中__init__,而不必将它们作为参数传递。我可以使用super()? 我找不到用父变量来做到这一点的方法。

python inheritance class

0
推荐指数
1
解决办法
30
查看次数

为什么 super() 调用可以在派生自 ABC 的 Python 类中工作?

我有一个带有具体方法的 Python ABC(我们称之为A__init__(),以及一个实现这个 ABC 的类(我们称之为B)。据我了解,我应该能够实例化 ABC。

问题:类函数super().__init__()内部的调用为什么以及如何工作?我假设创建了父类的一个实例 - 在本例中是 ABC。__init__Bsuper()

A级

from abc import ABC, abstractmethod
from util.fileManager import FileManager


class A(ABC):
    def __init__(self):
        self.file_manager = FileManager()  # Composition object

    ...
Run Code Online (Sandbox Code Playgroud)

B类,实现A类

from interfaces.A import A


class B(A):
    def __init__(self):
        super().__init__()

    ...
Run Code Online (Sandbox Code Playgroud)

编辑/解决方案

Python 中的“super”有什么作用?- super().__init__() 和显式超类 __init__() 之间的区别

正如你们许多人可能已经看到的那样,这个问题实际上可以归结为super()Python 中的作用。接受我标记的答案,我真的想指出@KarlKnechtel评论中的链接,因为它帮助我进一步了解情况。

python abc super python-3.x

0
推荐指数
1
解决办法
365
查看次数

-3
推荐指数
1
解决办法
1312
查看次数

标签 统计

python ×4

class ×2

inheritance ×2

python-3.x ×2

super ×2

abc ×1

oop ×1

python-3.5 ×1