function clone(object)--clone函数
local lookup_table = {}--新建table用于记录
local function _copy(object)--_copy(object)函数用于实现复制
if type(object) ~= "table" then
return object ---如果内容不是table 直接返回object(例如如果是数字\字符串直接返回该数字\该字符串)
elseif lookup_table[object] then
return lookup_table[object]--这里是用于递归滴时候的,如果这个table已经复制过了,就直接返回
end
local new_table = {}
lookup_table[object] = new_table--新建new_table记录需要复制的二级子表,并放到lookup_table[object]中.
for key, value in pairs(object) do
new_table[_copy(key)] = _copy(value)--遍历object和递归_copy(value)把每一个表中的数据都复制出来
end
return setmetatable(new_table, getmetatable(object))--每一次完成遍历后,就对指定table设置metatable键值
end
return _copy(object)--返回clone出来的object表指针/地址
end function clone(object)
local lookup_table = {}
local function _copy(object)
if type(object) ~= "table" then
return object
elseif lookup_table[object] then
return lookup_table[object]
end
local new_table = {}
lookup_table[object] = new_table
for key, value in pairs(object) do
new_table[_copy(key)] = _copy(value)
print(key,value)--这句添加print函数 演示输出复制的过程
end
return setmetatable(new_table, getmetatable(object))
end
return _copy(object)--返回clone出来的object表指针/地址
end
local a = {
[{100, 200}] = { 300, 400}, 200, { 300, 500},
abc = "abc"}
local myPolygon = {
color="blue",
thickness=2,
npoints=4,
{x=0, y=0},
{x=-10, y=0},
{x=-5, y=4},
{x=0, y=4}
}
local newtable = clone(a)
local newtable2 = clone(myPolygon)原文:http://blog.csdn.net/u013174689/article/details/41959011