设置元表后,填充__metatable字段,元表就无法被访问,依然可以被调用。
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
网友评论