#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)
#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/