lua(仿单继承)

--lua仿单继承
Account = { balance = 0}

function Account:new(o)
  o = o or {}
  setmetatable(o, self)--Account表本身作为o的metatable
  self.__index = self--自己作为自己的原型
  return o
end

function Account:deposit(v)
  self.balance = self.balance + v
end

function Account:withdraw(v)
  if v > self.balance then print("insufficient funds") end
  self.balance = self.balance - v
end

SpecialAccount = Account:new()--从Account继承所有操作

function SpecialAccount:withdraw(v)
  if v - self.balance >= self:getLimit() then
    print("insufficient funds")
  end
  self.balance = self.balance - v
end

function SpecialAccount:getLimit()
  return self.limit or 0
end


s = SpecialAccount:new{limit = 1000.00}--s继承SpecialAccount,SpecialAccount继承Account
s:deposit(100.00)

这个没怎么看懂

posted @ 2015-03-10 13:35  zzyoucan  阅读(182)  评论(0编辑  收藏  举报