【问题标题】:Python Classes exercise not working from parent classPython 类练习不适用于父类
【发布时间】:2021-06-28 01:19:11
【问题描述】:

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

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

1- 父类 Account 包含:

属性:

  • 文本类型名称
  • 整数型数量(账户中的钱)

还包含一个在 Account 类中返回数据的方法。

2- 定义两个类 FixedTime 和 Savings 继承自 Account 类,其中

储蓄类

  • 定义 init 传递从 Account 类继承的数据
  • 您将拥有一个打印信息的方法。

FixedTime 类将具有:

定义 init 来传递从 Account Class 继承的数据 拥有:

  • 属性词类型整数
  • 浮动利率属性。

方法:

  • 通过结果获取利息金额的方法 (金额*利息/100) 2. 持有人信息、数据、期限、利息、总金额的显示方式 兴趣。 创建一个主程序,在其中定义一个 FixedTime 和 Savings 类的变量, 填写必要信息并验证每个指定方法的功能。

我有以下代码:

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"Name: {self.name} term: {self.time} rate: {self.rate} Total rate: {self.total_import()}") AttributeError:“帐户”对象没有属性“时间”

【问题讨论】:

  • 你的问题是什么?顺便说一句,作业说这个字段应该叫quantity而不是amount,第三类应该是FixedTime,而不是Fixedrate
  • 您使用person.get_info(),而不是Saving.get_info(person),或者如果personSaving 对象,您将使用Saving 对象,但事实并非如此。您的意思是创建Saving 而不是Account?你写的是date.rate_info_print(),而不是Fixrate.rate_info_print(person)

标签: python class subclass


【解决方案1】:
  • 您需要在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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    • 1970-01-01
    • 2018-07-30
    • 1970-01-01
    • 2012-11-11
    相关资源
    最近更新 更多