The meta table (metatable) and meta Method (Metamethod) provided in Lua are a very important syntax, and metatable is primarily used to do something similar to the C + + overload operator.
 
The meta table provided in LUA is a personalized behavior to help the LUA variable complete some of the predefined features, such as the addition of two tables, which can be implemented by having both points to the same meta table and modifying the Meta method of the meta table.
 
Any table can be a meta table for any value, and a set of related tables can share a single meta table.
 
Some metamethod:
 
 
 
  
  Copy Code code as follows: 
 
 
  
 
 
__add (A, b) corresponding expression A + b 
 
__sub (A, B) corresponds to an expression a-b 
 
__mul (A, b) corresponding expression A * b 
 
__div (A, B) corresponds to the expression b/a 
 
__mod (A, b) corresponding to expression a% B 
 
__pow (A, b) corresponding expression a ^ b 
 
__UNM (a) corresponding expression-a 
 
__concat (A, B) corresponds to the expression A. B 
 
__len (a) corresponds to an expression #a 
 
__eq (A, b) corresponding expression A = = B 
 
__lt (A, b) corresponding expression a < b 
 
__le (A, b) corresponding expression a <= b 
 
__index (A, B) corresponds to an expression a.b 
 
__newindex (A, B, c) corresponds to an expression a.b = C 
 
__call (A, ...) corresponding expression A (...) 
 
 
 
 
1, the arithmetic class and relation class element method
 
Let's look at a simple example:
 
 
  
  Copy Code code as follows: 
 
 
  
 
  
  --We want to add two fractions, which is an undefined behavior. 
  
 
 
  Fraction_a = {numerator=2, denominator=3}
Fraction_b = {numerator=4, denominator=7}
fraction_op={}--meta table
 
  --__add This is metatable, this is the LUA build convention.
function Fraction_op.__add (a,b)
res={}
Res.numerator=a.numerator*b.denominator+b.numerator*a.denominator
Res.denominator=a.denominator*b.denominator
return res
End
 
  --Set the Fraction_a,fraction_b's meta table to Fraction_op
--where setmetatable is a library function
Setmetatable (FRACTION_A,FRACTION_OP)
Setmetatable (FRACTION_B,FRACTION_OP)
 
  --called the Fraction_op.__add () function
Fraction_c=fraction_a+fraction_b
Print (Fraction_c.numerator ...) /".. Fraction_c.denominator)
--Output results
--26/21
 
   
  
 
Let's look at an example of depth, example, the meta method of the arithmetic class, the meta method of the relational class, and the meta method of the library definition.
 
 
 
  
  Copy Code code as follows: 
 
 
  
 
 set={} 
 
 
  
 
 Local metatable={}--meta table
  
 
 --Creates a new collection from the values in the argument list
function Set.new (a)
Local set={}
--Assigns all Wenzu of the collection created by the method to the MetaTable
Setmetatable (set,metatable)
For i,v in pairs (a) do
Set[v]=true
End
return set
End
  
 
 --compute the set of two sets
function Set.union (a,b)
Local res=set.new{}
For I-pairs (a) do
Res[i]=true
End
For I-pairs (b) do
Res[i]=true
End
return res
End
  
 
 --computes the intersection of two sets
function Set.intersect (a,b)
Local res=set.new{}
For I-pairs (a) do
Res[i]=b[i]
End
return res
End
  
 
 --print always call tostring to format the output
--Here we do a little bit of modifying the library definition of print
function Set.tostring (a)
Local t={}
For I-pairs (a) do
t[#t +1]=i
End
Return "{" ... Table.concat (T, ",") ... "}"
End
  
 
 --Determine if a collection is a subset of the B set
function Set.lessorequal (a,b)
For I-pairs (a) do
If not B[i] then return False end
End
return True
End
  
 
 --Finally, the redirected meta method is added to the meta table
Metatable.__add=set.union
Metatable.__mul=set.intersect
Metatable.__tostring=set.tostring
Metatable.__le=set.lessorequal
Metatable.__eq=function (A,B) return a<=b and B<=a end
Metatable.__lt=function (A,B) return a<=b and not (b<=a) end
  
 
 s1=set.new{2,9,8,4}
s2=set.new{2,4,7}
S3=s1+s2
S4=s1*s2
Print (S3)
Print (S4)
Print (3+4,3*4)--The new method does not change the method that the table itself has, because the parameters passed in are different and only make the Meta method more perfect
s5=set.new{2,4}
s6=set.new{2,4,6}
Print (S5<=S6)
Print (S5<S6)
Print (S5==S6)
--Output results
--{2,8,4,9,7}
--{2,4}
--7 12
--true
--true
--false
  
  
 
2, table access to the Meta method:
 
Both the arithmetic class and the relational class's meta methods define behavior for their error conditions, they do not change the normal behavior of the language, but Lua provides a way to change the behavior of the table. There are two ways to change the behavior of a table: query table and modify a field that does not exist in the table.
 
1), __index element method
 
When you access a field that does not exist in the table, the result is nil. If we define the meta method __index for the table, the result of the access will be determined by the method.
 
 
 
  
  Copy Code code as follows: 
 
 
  
 
  
  window={} 
  
WINDOW.PROTOTYPE={X=10,Y=20,WIDTH=100,HEIGHT=200} 
  
Wenzu of window.mt={}--window 
  
 
 
  function Window.new (o)
Setmetatable (O,WINDOW.MT)
Return o
End
 
  Window.mt.__index=function (Table,key) return Window.prototype[key] End
 
  W=WINDOW.NEW{X=1,Y=22}
Print (W.width)
Print (W.WIDTH1)
--Output results
--100
--nil
 
   
  
 
2), __newindex element method
 
Unlike __index, the meta method is used to assign values that do not exist for the key, and the former is used for access.
 
 
 
  
  Copy Code code as follows: 
 
 
  
 
  
  window={} 
  
WINDOW.PROTOTYPE={X=10,Y=20,WIDTH=100,HEIGHT=200} 
  
Wenzu of window.mt={}--window 
  
 
 
  function Window.new (o)
Setmetatable (O,WINDOW.MT)
Return o
End
 
  Window.mt.__index=function (Table,key) return Window.prototype[key] End
Window.mt.__newindex=function (table,key,value) Window.prototype[key]=value end
 
  W=WINDOW.NEW{X=1,Y=22}
W.length=50
Print (W.width)
Print (W.WIDTH1)
Print (Window.prototype.length)
--Output results
--100
--nil
--50