class BA(object):
'''
银行帐户 类
属性:
name 帐户名
balance 余额
方法:
SaveMoney 存钱
DrawMoney 取钱
GetBalance 余额查询
Transfer 转帐
'''
def __init__(self, name, balance):
self.name = name
self.__balance = balance
# 存钱
def SaveMoney(self, money):
self.__balance += money
#print('存入:{0},余额:{1}'.format(money, self.__balance))
# 取钱
def DrawMoney(self, money):
if self.__balance >= money:
self.__balance -= money
#print('取出:{0},余额:{1}'.format(money, self.__balance))
else:
print('余额不足!')
# 获取帐户余额,由于设置__balance为私有属性,外部不能直接访问
def GetBalance(self):
return ('余额:{}'.format(self.__balance))
# 转帐
def Transfer(self, ba, money):
if self.name == ba.name:
print('同一帐户不能进行转帐!')
else:
if self.__balance >= money:
self.DrawMoney(money)
ba.SaveMoney(money)
print('转帐到 {0} {1},余额:{2}'.format(ba.name, money, self.__balance))
else:
print('余额不足!')
zhang = BA('zhang', 1000)
wang = BA('wang', 1000)
zhang.Transfer(wang, 500)
print('wang 帐户余额:'+wang.GetBalance())
运行结果:
转帐到 wang 500,余额:500
wang 帐户余额:余额:1500
感谢 冰风漫天 回答论坛提问 https://bbs.csdn.net/topics/395627880
新增子类,搜索一圈,发现好像python3继承上,子类只能继承父类的私有属性(私有方法未测试),而没有权限对父类私有属性进行赋值或修改,如要进行相关操作还需在父类中创建方法,太麻烦,如果没必要还是尽量少用私有属性。以下例子中把__balance私有属性改为公有属性。
class BA(object):
'''
银行帐户 类
属性:
name 帐户名
balance 余额
方法:
SaveMoney 存款
DrawMoney 取款
GetBalance 余额查询
Transfer 转帐
'''
# 属性
def __init__(self, name, balance):
self.name = name
self.balance = balance
# 存钱
def SaveMoney(self, money):
self.balance += money
print('帐户 {2} 存入:{0},余额:{1}'.format(money, self.balance, self.name))
# 取钱
def DrawMoney(self, money):
if self.balance >= money:
self.balance -= money
print('帐户 {2} 取出:{0},余额:{1}'.format(
money, self.balance, self.name))
else:
print('帐户 {0} 余额不足! 取款:{1}, 余额:{2}'.format(
self.name, money, self.balance))
# 获取帐户信息
def GetBankAccount(self):
return (print('帐户名: {0}, 余额: {1}'.format(self.name,self.balance)))
# 转帐
def Transfer(self, ba, money):
print('转帐中... ...')
if self.name == ba.name:
print('同一帐户不能进行转帐!')
else:
if self.balance >= money:
self.DrawMoney(money)
ba.SaveMoney(money)
print('转帐成功!')
else:
print('帐户:{0} 余额不足! 转帐 {1}, 余额:{2}'.format(
self.name, money, self.balance))
# 子类
class CreditCard(BA):
'''
信用卡 银行帐户BA子类
属性:
credit 信用额
overdraft 透支, 0代表未透支, 1代表已透支
方法:
DrawMoney 取款
CheckCard 信用卡帐户查询
'''
# 属性
def __init__(self, name, balance, credit, overdraft):
BA.__init__(self, name, balance)
self.credit = credit
self.overdraft = overdraft
# 取款, 改写父类DrawMoney方法
def DrawMoney(self, money):
if self.balance+self.credit >= money:
if self.balance >= money:
self.balance -= money
else:
self.credit = self.credit+self.balance-money
self.balance = 0
self.overdraft = 1
print('帐户: {0}, 取款: {1}, 余额: {2}, 信用额: {3}, 是否透支: {4}'.format(
self.name,money,self.balance,self.credit,self.overdraft))
else:
print('帐户 {0} 额度不足! 取款金额: {1}, 帐户余额:{2}, 信用额: {3}'.format(
self.name, money, self.balance, self.credit))
# 信用卡帐户查询
def GetCreditCard(self):
return (print('帐户名: {0}, 余额: {1}, 信用额: {2}, 是否透支: {3}'.format(
self.name, self.balance, self.credit, self.overdraft)))
# 运行
zhang=BA('zhang',1000)
wang=BA('wang',1000)
#查询初始帐户情况
print('银行帐户初始信息:')
zhang.GetBankAccount()
wang.GetBankAccount()
#存款操作
print('存款操作.')
zhang.SaveMoney(1000)
#转帐操作
print('转帐操作.')
zhang.Transfer(wang,1500)
#取款操作
print('取款操作.')
wang.DrawMoney(2000)
#信用卡帐户
li = CreditCard('li', 1000, 1000, 0)
print("信用卡帐户初始信息:")
li.GetCreditCard()
#取款操作
print('信用卡帐户取款操作.')
li.DrawMoney(500)
li.DrawMoney(700)
li.DrawMoney(1000)
#存款操作
print('信用卡帐户存款操作.')
li.SaveMoney(1000)
#帐户查询
print('帐户查询操作.')
li.GetBankAccount()
li.GetCreditCard()
运行结果:
银行帐户初始信息:
帐户名: zhang, 余额: 1000
帐户名: wang, 余额: 1000
存款操作.
帐户 zhang 存入:1000,余额:2000
转帐操作.
转帐中... ...
帐户 zhang 取出:1500,余额:500
帐户 wang 存入:1500,余额:2500
转帐成功!
取款操作.
帐户 wang 取出:2000,余额:500
信用卡帐户初始信息:
帐户名: li, 余额: 1000, 信用额: 1000, 是否透支: 0
信用卡帐户取款操作.
帐户: li, 取款: 500, 余额: 500, 信用额: 1000, 是否透支: 0
帐户: li, 取款: 700, 余额: 0, 信用额: 800, 是否透支: 1
帐户 li 额度不足! 取款金额: 1000, 帐户余额:0, 信用额: 800
信用卡帐户存款操作.
帐户 li 存入:1000,余额:1000
帐户查询操作.
帐户名: li, 余额: 1000
帐户名: li, 余额: 1000, 信用额: 800, 是否透支: 1