与小卡特一起学python 第14章 对象 动手试一试

本文介绍了使用Python实现银行账户类的基本操作,包括存款、取款及显示余额,并进一步展示了如何通过继承来创建带有利息计算功能的账户。通过具体实例演示了这些功能的使用。
#14章对象动手试一试
#14.1对应银行账户的类如下所示:
class BankAccout:
    def __init__(self,acct_number,acct_name):
        self.acct_number = acct_number
        self.acct_name = acct_name
        self.balance = 0.0
    def displayBalance(self):
        print("The account balance is:",self.balance)

    def deposit(self,amount):
        self.balance = self.balance + amount
        print("You deposited",amount)
        print("The new balance is:",self.balance)
    def withdraw(self,amount):
        if self.balance >=amount:
            self.balance = self.balance - amount
            print ("You withdrew",amount)
            print ("The new balance is :",self.balance)
        else:
            print("You tried to withdraw",amount)
            print("The account balance is:",self.balance)
            print("Withdrawal denied. Not enough funds.")
myAccount = BankAccout(234567,"Warren Sande")
print("Account name:",myAccount.acct_name)
print("Account number:",myAccount.acct_number)
myAccount.displayBalance()

myAccount.deposit(34.52)
myAccount.withdraw(12.25)
myAccount.withdraw(30.8)

#14.2建立一个账户信息,计算利息
##class InterestAccount(BankAccount):
##    def __init__(self,acct_number,acct_name,rate):
##        BankAccount.__init__(self,acct_number,acct_name)
##        self.rate = rate
##    def addInterest (self):
##        interest = self.balance * self.rate
##        print("adding interest to the account,",self.rate * 100,"percent")
##        self.deposit (interest)

class BankAccount:
    def __init__(self, acct_number, acct_name):
        self.acct_number = acct_number
        self.acct_name = acct_name
        self.balance = 0.0

    def displayBalance(self):
        print( "The account balance is:", self.balance)

    def deposit(self, amount):
        self.balance = self.balance + amount
        print( "You deposited", amount)
        print( "The new balance is:", self.balance)
        
    def withdraw(self, amount):
        if self.balance >= amount:
            self.balance = self.balance - amount
            print( "You withdrew", amount)
            print( "The new balance is:", self.balance)
        else:
            print ("You tried to withdraw", amount)
            print ("The account balance is:", self.balance)
            print("Withdrawl denied.  Not enough funds.")

class InterestAccount(BankAccount):
    def addInterest(self, rate):
        interest = self.balance * rate
        print( "adding interest to the account,",rate * 100,"percent")
        self.deposit (interest)
myAccount = InterestAccount(234567, "Warren Sande")
print ("Account name:", myAccount.acct_name)
print( "Account number:", myAccount.acct_number)
myAccount.displayBalance()
myAccount.deposit(34.52)
myAccount.addInterest(0.11)

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/220205/viewspace-2076519/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/220205/viewspace-2076519/

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值