首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >银行账户子类OOP

银行账户子类OOP
EN

Stack Overflow用户
提问于 2021-02-09 07:41:33
回答 2查看 396关注 0票数 0

BankAccount类应该有两个子类,名为SavingsAccount和NonTaxFilerAccount。SavingsAccount类应该有一个ZakatDeduction( )函数,它在调用时扣除经常帐户余额的2.5%。NonTaxFilerAccount类应该覆盖父类的函数。从账户中扣除2%的预扣税的,每次提取提梅。

我做了第一部分,但没有得到第二部分,它一直给我属性错误。

代码语言:javascript
复制
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)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-02-09 18:15:43

我想你有几个问题。基本上,BankAccount、SavingsAccount和NonTaxFilerAccount类的实现在结构上是正确的。然而:

  1. 由于指令规定每次调用ZakatDeduction时将帐户余额减少2.5%,因此您应该更新该方法以删除金额如下:

代码语言:javascript
复制
    def ZakatDeduction(self):
        self.account -= self.account*0.025
        print(self.account)  

  1. ,因为指令中说,当NonTaxFiler生成withdraw3al时,要将帐户余额再减少2%,那么您应该按以下方式更新NonTaxFiler取款方法:

代码语言:javascript
复制
    def withdraw(self, amount):
       self.account -= amount*(1.02)
       print(self.account)

雇用这些类来建立单独的帐户,并有700项余额,如下所示:

代码语言:javascript
复制
BA = BankAccount(700)     #Create a base account
SA = SavingAccount(700)   #Create a Savings Account
NTF = NonTaxFiler(700)    #Create a NonTaxFilerAccount

然后执行以下操作:

代码语言:javascript
复制
BA.withdraw(25)
BA.balance()
675  

SA = SavingsAccount(700)
SA.ZakatDeduction()
682.5  

NTF = NonTaxFilerAccount(700)
NTF.withdraw(25)
674.5
票数 0
EN

Stack Overflow用户

发布于 2021-02-09 18:43:39

属性错误是正确的,实际上它的消息告诉您问题所在。你的课没问题。错误在使用中。你有:

代码语言:javascript
复制
y=BankAccount
y.SavingsAccount(700)

这意味着y变量现在引用了BankAccount 。下一行尝试调用y.SavingsAccount,而BankAccount类没有一个名为SavingsAccount的方法。

你的意思是:

代码语言:javascript
复制
y = SavingsAccount(700)

注意,python是特定于空格的。虽然在技术上是有效的,但为了可读性,您应该在任何地方都使用相同级别的缩进,但有些方法缩进4,另一些则缩进3。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66114808

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档