Python 无法使用_str显示数据__

Python 无法使用_str显示数据__,python,class,oop,inheritance,Python,Class,Oop,Inheritance,此代码是较大程序的一部分。。我试图生成一个摘要,显示从用户处获取的数据 class Mbilling: def __init__(self, income, debit = 0, rent = 745): self.income = income self.debit = debit self.rent = rent class Display(Mbilling): def listMbilling(self):

此代码是较大程序的一部分。。我试图生成一个摘要,显示从用户处获取的数据

class Mbilling:
   def __init__(self, income, debit = 0, rent = 745):
       self.income = income
       self.debit   = debit
       self.rent    = rent

class Display(Mbilling):

   def listMbilling(self):
       self.m = f"""
            ------------------------------------
            --> Income = {self.income}
            --> Debit  = {self.debit}
            --> Rent   = {self.rent}
            ------------------------------------
                 """

    def __str__(self):
        return self.m
我得到了这个错误:

Traceback (most recent call last):
  File "G:\Engineering\#ZicX\#Programming in Python\Basic Python\Tutorials\test.py", line 25, in <module>
    receipt = Display()
TypeError: __init__() missing 1 required positional argument: 'income'

需要帮助才能了解问题所在,谢谢

我觉得这里的问题是类继承的问题。我决不是一个超级专家,但以下是我可以贡献的:

以下是一个例子:

class MethodOne:

def __init__(self):

    self.param_one = "Car"
    self.param_two = 200


class MethodTwo(MethodOne):

    def __init__(self):
        super().__init__()

    def __str__(self):
        self.string_one = f"I have a {self.param_one}." 
        self.string_two = f"{self.param_two} minus 100 equals {self.param_two - 100}!"
        

        return self.string_one + "\n" + self.string_two
这是另一个与你期望实现的目标更密切相关的例子

class Mbilling:

    def __init__(self, income = 0, debit = 0, rent = 745):
        self.income = income
        self.debit = debit
        self.rent = rent

        print("Hi there. I am the parent")

class Display(Mbilling):

    def __init__(self, income, debit, rent):
        print("Hi, I am the child")
        
        super().__init__()

    
    def listMbilling(self):
        self.m = f"""
            ------------------------------------
            --> Income = {self.income}
            --> Debit  = {self.debit}
            --> Rent   = {self.rent}
            ------------------------------------
                 """
    
    def __str__(self):
        self.listMbilling()
        return self.m

d1 = Display(23, 40, 745)
print(d1)

而且,此链接是超级链接;)有用的https://www.educative.io/edpresso/what-is-super-in-python

您正在创建一个
Display()
,而没有传递所需的参数。我想在最后一段代码中,您是想将参数
5000、0750
传递到
Display()
而不是
Mbilling
。也许可以对类的工作原理做一些基本的了解。不应该让类Display()继承这些属性来形成超类吗?当你创建一个
Display
实例时,类不是这样工作的,您必须将类
Mbilling
期望的参数传递给它,因为子类正在继承
Mbilling.\uuu init\uuu()
方法。
receive=Display(…)
Mbilling
。它不是刚才创建的
Mbilling
。您仍然必须向它提供
Mbilling
所需的所有数据。请注意
显示。\uuuu init\uuuu
丢弃了它的参数。它应该将它们传递给其父级
\uuuu init\uuu
。此代码之所以有效,是因为
Mbilling
已更改为无输入工作,而不是因为类
Display
Display(23、40、745)
正确处理参数。
class Mbilling:

    def __init__(self, income = 0, debit = 0, rent = 745):
        self.income = income
        self.debit = debit
        self.rent = rent

        print("Hi there. I am the parent")

class Display(Mbilling):

    def __init__(self, income, debit, rent):
        print("Hi, I am the child")
        
        super().__init__()

    
    def listMbilling(self):
        self.m = f"""
            ------------------------------------
            --> Income = {self.income}
            --> Debit  = {self.debit}
            --> Rent   = {self.rent}
            ------------------------------------
                 """
    
    def __str__(self):
        self.listMbilling()
        return self.m

d1 = Display(23, 40, 745)
print(d1)