微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Python3:抽象类的子类是否继承其魔术方法?

如何解决Python3:抽象类的子类是否继承其魔术方法?

这是茶:我正在使用 python 编写一个小型大富翁游戏。我让这个小家庭来代表账单。有一个名为 Bill 的基 abstract class,它继承自 abc.ABC 类。

代码

from abc import ABC,abstractmethod
import colorama
colorama.init(autoreset=True,strip=True)
class Bill(ABC):
  #Abstract Properties (must be overriden in subclasses)
  @property
  @abstractmethod
  def count(self):
    return 0
  @property
  @abstractmethod
  def color(self): # colorama background color
    return 0
  @property
  @abstractmethod
  def worth(self): # integer representing the bill's worth
    return 0

  # Dunder methods (The same across class family)
  def __add__(self,other):
    return self.worth + other.worth
  def __str__(self):
    return f" {self.color} {self.worth}"

class One(Bill):
  def __init__(self):
    self.__count = 0
    self.__color = "\033[0m"
    self.__worth = 0
    #super().init() ??
  # Override Abstract Methods
  @property
  def count(self):
    return self.__count
  @count.setter
  def count(self,amount):
    self.__count += 1
  @property
  def worth(self):
    return 1
  @property
  def color(self):
    return colorama.Back.WHITE

我的问题是我的子类是否会继承 Bill 类的 dunder 方法。 如果没有,我是否需要在 super().__init__() 类的 One 方法中包含 __init__

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。