Account ={Balance=0}functionaccount:deposit (v) self.balance= Self.balance +vEndfunctionaccount:new (o) o= Oor {} setmetatable(O, self)--the Account table itself as the metatable of OSelf.__index = Self--yourself as a prototype?? returnoEnda= Account:new{balance =0}--call A of the deposit, he will find a metametable of the _index corresponding table--getmetatable (a). __index (A, b)--The final call is the account's deposit, which is account.deposit (A, 100.00)A:deposit (100.00)--This method invokes the deposit of the original account.--The default value of balance will be inheritedb =account:new ()Print(b.balance)--b.balance equals 0, the next time this value is accessed, the __index,b already exists in its own balance domain--In this way, you can simulate object-oriented, the account is a class, and the created B is the object of this class.
Lua (Imitation Class)