ITPub博客

首页 > 应用开发 > IT综合 > 与小卡特一起学python 第14章 对象 动手试一试

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

原创 IT综合 作者:yarking207 时间:2016-04-07 00:02:28 0 删除 编辑
#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博客 ” ,链接:https://blog.itpub.net/220205/viewspace-2076519/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2011-08-03

  • 博文量
    65
  • 访问量
    99302