與小卡特一起學python 第14章 物件 動手試一試
#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/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 與小卡特一起學Python 第15章 模組 及動手試一試Python
- 與小卡特一起學python 第8章 動手試一試原始碼Python原始碼
- 與小卡特一起學python 第14章 物件Python物件
- 與小卡特一起學python 第13章 函式-積木 動手試一試Python函式
- 與小卡特一起學python 第5章 輸入 測試題和動手試一試Python
- 與小卡特一起學python 第12章 收集起來,列表與字典 動手試一試Python
- 與小卡特一起學python 第11章 巢狀與可變迴圈 動手試一試Python巢狀
- 與小卡特一起學python 第6章 gui-圖形使用者介面 測試和動手試一試PythonGUI
- 與小卡特一起學python 第2章 記住記憶體和變數 課後 動手試一試Python記憶體變數
- 與小卡特一起學python 第19章 聲音Python
- 與小卡特一起學python 第20章 使用pyqtPythonQT
- 與小卡特一起學python 第16章 圖形 Pygame學習PythonGAM
- 與小卡特一起學python 第13章 函式-積木Python函式
- 與小卡特一起學python 第3章 基本數學運算Python
- 與小卡特一起學python 第21章 列印格式化與字串Python字串
- 與小卡特一起學python 第4章 資料的型別Python型別
- 與小卡特一起學python 第11章 巢狀與可變迴圈Python巢狀
- 與小卡特一起學python 第22章 檔案輸入與輸出Python
- 與小卡特一起學python 第18章 一種新的輸入-事件Python事件
- 與小卡特一起學python 第9章 全都為了你-註釋Python
- 與小卡特一起學python 第17章動畫精靈和碰撞檢測Python動畫
- 與小卡特一起學python 第10章 遊戲時間到了 程式碼清單Python遊戲
- 與小卡特一起學python 第1章 出發吧 課後練習題Python
- 與小卡特一起學python 第1章 出發吧 1-2猜數遊戲Python遊戲
- 與小卡特一起學python 第8章 轉圈圈 FOR迴圈和條件迴圈Python
- 與小卡特一起學python 第7章 判斷再判斷 7-1-2-3-6-7Python
- 與小卡特一起學python 第2章 記住記憶體和變數 2-1練習Python記憶體變數
- 與小卡特一起學python 第5章 輸入 5-1,2,3,4 input()輸入函式Python函式
- 與小卡特一起學python 第10章 遊戲時間到了 pygame安裝及素材圖片準備Python遊戲GAM
- 與小卡特一起學python 第1章 出發吧 1-1練習我們第一個真正的程式Python
- 與小卡特一起學python 第6章 gui-圖形使用者介面 6-1-2-3-4-5 gui使用PythonGUI
- 《Web介面開發與自動化測試基於Python語言》--第11章WebPython
- 動手學習資料分析第1章
- 動手學習資料分析 第2章
- 第1章 軟體測試概述線上測試
- 一起玩轉微服務(14)——單元測試微服務
- ElasticSearch學習一-小試牛刀Elasticsearch
- 《Spring 手擼專欄》第 2 章:小試牛刀(讓新手能懂),實現一個簡單的Bean容器SpringBean