一、单继承---基类---普通银行账户,只能取自己存的钱,相当于储蓄卡Account = { id = -1, balance = 0}function Account:new(t) t = t or {} setmetatable(t, self) self.__index = self return tendfunction Account:deposite(x...
摘要由CSDN通过智能技术生成
一、单继承
---基类
---普通银行账户,只能取自己存的钱,相当于储蓄卡
Account = {
id = -1,
balance = 0
}
function Account:new(t)
t = t or {}
setmetatable(t, self)
self.__index = self
return t
end
function Account:deposite(x)
self.balance = self.balance + x
end
function Account:withdraw(x)
print("try to withdraw "..x)
if x > self.balance then
print("balance not enough!")
else
print("withdraw success")
self.balance = self.balance - x
end
end
function Account:check()
print("your account has "..self.balance)
end
---派生类
--特殊银行账户,可以预支,相当于信用卡
SpecialAccount = Account:new()
function SpecialAccount:withdraw(x)
print("try to withdraw "..x)
if x > self.balance + self.limit then
print("balance and limit left not enough!")
elseif x <= self.balance then
print("withdraw success from account ")
self.balance = self.balance