TableA = {}
function TableA:f1()
print("TableA")
end
mt = {}
function mt:__tostring()
return "mt to string"
end
function mt:f2()
print("mt")
end
mt__index = {}
function mt__index:f2()
print("mt__index");
end
mt.__index = mt__index
setmetatable(TableA, mt)
TableA:f1();
TableA:f2();
print(TableA)
输出
TableA
mt__index
mt to string
Program in LUA中老是把metatable的真实实现与面向对象中继承实现搞混淆
setmetatable(o, self)
self.__index = self