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)

这个没怎么看懂

原文地址:https://www.cnblogs.com/zzyoucan/p/4325804.html