定义类Array:
function Array:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
o.content = {} --此处再赋值创建空表
return o;
end
Array = {
nextIdx = 1,
nextIdx = 1,
content = {}
}
function Array:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o;
end
}
function Array:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o;
end
测试:
a = Array:new()
b = Array:new()
a. nextIdx = 10
print(a.nextIdx)
print(b.nextIdx)
print(a.content)
print(b.content)
输出为
10
1
table: 0021D528
table: 0021D528
发现a与b的nextIdx属性不同,但content值相同,为同一个表
但是在new时再为其幅值可避免这一问题,将其修改为
Array = {
nextIdx = 1,
nextIdx = 1,
content = nil
}
}
o = o or {}
setmetatable(o, self)
self.__index = self
o.content = {} --此处再赋值创建空表
return o;
end
调用同样测试函数结果为:
10
1
0023D668
0023D528
结果各不相同