function newAccount(self) self = self or {} local balance = 0 -- private function self.balance() return balance end function self.deposit(v) balance = balance + v end function self.withdraw(v) balance = balance - v end return self end a = newAccount() a.deposit(100) print(a.balance()) a.withdraw(20) print(a.balance()) a.withdraw(20) print(a.balance()) print() function newSpecialAccount(self) self = self or {} self = newAccount(self) function self.getLimit() return self.limit or 0 end local superWithdraw = self.withdraw function self.withdraw(v) if self.balance() - v < self.getLimit() then print "insufficient funds" else superWithdraw(v) end end return self end a = newSpecialAccount() a.limit = 70 a.deposit(100) print(a.balance()) a.withdraw(20) print(a.balance()) a.withdraw(20) print(a.balance())