B) 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)
答案 0 :(得分:0)
我认为您有几个问题。基本上,您对 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
答案 1 :(得分:0)
属性错误是正确的,实际上它的消息告诉您问题所在。你的课没问题。错误在于用法。你有:
y=BankAccount
y.SavingsAccount(700)
这意味着 y
变量现在指的是 BankAccount
类。下一行尝试调用 y.SavingsAccount
,而 BankAccount
类没有名为 SavingsAccount
的方法。
您可能是说:
y = SavingsAccount(700)
请注意,python 是特定于空格的。虽然技术上有效,但为了可读性,你应该在任何地方使用相同级别的缩进,但你的一些方法缩进 4,而其他方法缩进 3