設(shè)置元表后,填充__metatable字段,元表就無法被訪問,依然可以被調(diào)用。
Set.mt = {}
Set.mt.__add = Set.union
function Set.new(t)
local set = {}
setmetatable(set,Set.mt)
for _,l in ipairs(t) do
set[l] = true
end
return set
end
Set.mt.__metatable = "not your businesss"
s1 = Set.new{}
print(getmetatable(s1))? ?-- 顯示 not your business
setmetatable(s1,{})? ?-- cannot change a protected metatable