http://blog.csdn.net/ssihc0/article/details/7742323
Account={balance=0}; --Create a new pair like, he has an attribute Balancefunction account:New(o)--here:New(o) in the colon, the code can omit self, at the time of the Access object:New(o) If it is a point number:New(o) should be changed.New(Self,o) at the time of the visitObject. (Object, O) o=o or {} setmetatable (o,self);--O inherit from self self.__index=self;--Associated meta-table entriesreturno;endfunction account.deposit (self,v) self.balance=self.balance+v;endfunction Account:withdraw (v)if(v) > self.balance then Error"Insufficient funds"; end Self.balance=self.balance-V;endspecialaccount=account:New(); --create ANew Object class, Basic accountclasss=specialaccount:New{limit=1000.00};--instantiate a new Specialaccount object print (s.balance); S:deposit (100.00); function Specialaccount:withdraw (v)ifV-self.balance >=self:getlimit () then error"Insufficient funds"; End Self.balance=self.balance-v;endfunction specialaccount.getlimit (self)returnSelf.limit or0; EndPrint (s.limit);p rint (S.getlimit (s)) print (s.balance)
Lua class inheritance and implementation