Python 类 从 parent class 起练习无效

Python Classes exercise not working from parent class

我有以下练习,但我无法完全理解如何让它发挥作用。

开发一个包含以下内容的程序:

1- 一个 parent class 帐户包含:

属性:

还包含一个方法,该方法 returns 帐户 class 中的数据。

2- 定义从帐户 class 继承的两个 classes FixedTime 和 Savings,其中

储蓄class

FixedTime class 将有:

定义 init 传递从帐户 Class 继承的数据 拥有:

方法:

我有以下代码:

class Account:
    def __init__(self, name, amount):
        self.name = name
        self.amount = amount

    def get_name(self):
        return self.name
    def get_amount(self):
        return self.amount

class Saving(Account):
    def __init__(self, name, amount):
        Account.__init__(self, name, amount)
    def  get_info(self):
        print(f"{self.name}, {self.amount}")

class Fixrate(Account):
    def __init__(self, rate, time):
        self.rate = rate
        self.time = time
    def total_import(self):
        total = (self.amount * self.rate)/ 100
        return total
    def rate_info_print(self):
        print(f"Name: {self.name} term: {self.time} rate: {self.rate} Total rate: {self.total_import()}")


person = Account("andres", 5000)
date = Fixrate(4.5, 4)
Saving.get_info(person)
Fixrate.rate_info_print(person)

我收到这个错误:

print(f"姓名:{self.name} 任期:{self.time} 比率:{self.rate} 总比率:{self.total_import()}") AttributeError: 'Account' object 没有属性 'time'

  • 您需要在 Fixrate __init__
  • 中分配 timename
  • 您为 Fixrate 创建了一个名为 date 的实例,那么您应该使用它而不是使用 Fixrate.rate_info_print(person)

代码:

class Account:
    def __init__(self, name, amount):
        self.name = name
        self.amount = amount

    def get_name(self):
        return self.name
    def get_amount(self):
        return self.amount

class Saving(Account):
    def __init__(self, name, amount):
        super().__init__(name, amount)
    def get_info(self):
        print(f"{self.name}, {self.amount}")

class Fixrate(Account):
    def __init__(self, rate, time, name, amount):
        self.rate = rate
        self.time = time
        super(Fixrate, self).__init__(name,amount)

    def total_import(self):
        total = (self.amount * self.rate)/ 100
        return total
        
    def rate_info_print(self):
        print(f"Name: {self.name} term: {self.time} rate: {self.rate} Total rate: {self.total_import()}")


person = Account("andres", 5000)
date = Fixrate(4.5, 4,"andres",5000)
Saving.get_info(person)
date.rate_info_print()

结果:

andres, 5000
Name: andres term: 4 rate: 4.5 Total rate: 225.0

或者如果你想使用person,你可以试试这个。

代码:

class Account:
    def __init__(self, name, amount):
        self.name = name
        self.amount = amount

    def get_name(self):
        return self.name
    def get_amount(self):
        return self.amount

class Saving(Account):
    def __init__(self, name, amount):
        super().__init__(name, amount)
    def get_info(self):
        print(f"{self.name}, {self.amount}")

class Fixrate(Account):
    def __init__(self, rate, time, person):
        self.rate = rate
        self.time = time
        self.person = person
        # super(Fixrate, self).__init__(name,amount)

    def total_import(self):
        total = (self.amount * self.rate)/ 100
        return total
        
    def rate_info_print(self):
        self.name = person.get_name()
        self.amount = person.get_amount()
        print(f"Name: {self.name} term: {self.time} rate: {self.rate} Total rate: {self.total_import()}")


person = Account("andres", 5000)
date = Fixrate(4.5, 4,person)
Saving.get_info(person)
date.rate_info_print()

结果:

andres, 5000
Name: andres term: 4 rate: 4.5 Total rate: 225.0