lua元表,B设置为A的元表
2022-09-05 本文已影响0人
好怕怕
local config = {a=1} -- 普通表
local propConfig = {b=2,c="元表的值"} -- 元表
setmetatable(config,{__index = propConfig})
print(config.a)
print(config.c)
https://c.runoob.com/compile/66/

local config = {a=1}
local propConfig = {c=344}
local cfg = setmetatable(config,{__index = function(tab,key)
if propConfig[key] then
return propConfig[key]
else
return "没有值啊!"
end
end})
print(cfg.a)
print(cfg.ca)