function clone(tab)
-- 判空
if tab == nil then
return nil;
end
-- 判断类型是否是表
if type(tab) ~= "table" then
return tab;
end
-- 创建一个新表
local newTable = {};
-- 设置元表
local oldMetaTable = getmetatable(tab);
if oldMetaTable ~= nil then
setmetatable(newTable, oldMetaTable);
end
-- 遍历表中的元素赋值到新表
for key, value in pairs tab do
if type(value) == "table" then
newTable[key] = clone(value);
else
newTable[key] = value;
end
end
return newTable;
end
lua深拷贝代码实现
于 2025-03-12 21:35:33 首次发布
2101

被折叠的 条评论
为什么被折叠?



