Zi 字媒體
2017-07-25T20:27:27+00:00
PYTHON機器學習自學/自修 整理[00018] ~ 語言技術:PYTHON GOSSIP(定義類別)
import sys
import decimal#精準度/精度 運算
import random
class Account:#定義一個帳戶(Account)類別
#建構子
def __init__(self, number, name):
#直接宣告成員變數並且直接使用
self.number = number#編號
self.name = name#姓名
self.balance = 0#結餘
#成員函數
def deposit(self, amount):#存款
if amount <= 0:
raise ValueError('must be positive')#程式直接報錯,不會繼續執行
self.balance += amount
def withdraw(self, amount):#撤(領錢)
if amount <= self.balance:
self.balance -= amount
else:
raise RuntimeError('balance not enough')
def main():
Account01=Account('B0001', "jash.liao")#宣告物件(呼叫建構子)
print(Account01.number+','+Account01.name+","+str(Account01.balance));
Account01.deposit(100)
#Account01.deposit(-100)
print(Account01.number+','+Account01.name+","+str(Account01.balance));
Account01.withdraw(50)
#Account01.withdraw(100)
print(Account01.number+','+Account01.name+","+str(Account01.balance));
main()
寫了
5860316篇文章,獲得
23313次喜歡