BankAccount类应该有两个子类,名为SavingsAccount和NonTaxFilerAccount。SavingsAccount类应该有一个ZakatDeduction( )函数,它在调用时扣除经常帐户余额的2.5%。NonTaxFilerAccount类应该覆盖父类的函数。从账户中扣除2%的预扣税的,每次提取提梅。
我做了第一部分,但没有得到第二部分,它一直给我属性错误。
class BankAccount:
def __init__(self, init_bal):
"""Creates an account with the given balance."""
self.init_bal = init_bal
self.account = init_bal
def deposit(self, amount):
"""Deposits the amount into the account."""
self.amount = amount
self.account += amount
def withdraw(self, amount):
self.account -= amount
def balance(self):
print (self.account)
class SavingsAccount(BankAccount) :
def ZakatDeduction(self,amount):
self.account=amount*0.25
print(self.account)
class NonTaxFilerAccount(BankAccount):
def withdraw(self, amount):
self.account -= amount*(0.2)
print(self.account)
x = BankAccount(700)
x.balance()
y=BankAccount
y.SavingsAccount(700)
z=BankAccount
z.withdraw(70)发布于 2021-02-09 18:15:43
我想你有几个问题。基本上,BankAccount、SavingsAccount和NonTaxFilerAccount类的实现在结构上是正确的。然而:
def ZakatDeduction(self):
self.account -= self.account*0.025
print(self.account) def withdraw(self, amount):
self.account -= amount*(1.02)
print(self.account)雇用这些类来建立单独的帐户,并有700项余额,如下所示:
BA = BankAccount(700) #Create a base account
SA = SavingAccount(700) #Create a Savings Account
NTF = NonTaxFiler(700) #Create a NonTaxFilerAccount然后执行以下操作:
BA.withdraw(25)
BA.balance()
675
SA = SavingsAccount(700)
SA.ZakatDeduction()
682.5
NTF = NonTaxFilerAccount(700)
NTF.withdraw(25)
674.5发布于 2021-02-09 18:43:39
属性错误是正确的,实际上它的消息告诉您问题所在。你的课没问题。错误在使用中。你有:
y=BankAccount
y.SavingsAccount(700)这意味着y变量现在引用了BankAccount 类。下一行尝试调用y.SavingsAccount,而BankAccount类没有一个名为SavingsAccount的方法。
你的意思是:
y = SavingsAccount(700)注意,python是特定于空格的。虽然在技术上是有效的,但为了可读性,您应该在任何地方都使用相同级别的缩进,但有些方法缩进4,另一些则缩进3。
https://stackoverflow.com/questions/66114808
复制相似问题