表
tab = {}
tab.k1 = 1
tab.k2 = 2
print(tab.k1) --输出 1
print(tab["k2"]) --输出 2
print(tab['k1']) --输出 1
--哈希表形式,无[i]
print(tab[1]) --输出 nil
--哈希表形式,无法获取长度,仅数组可用
print("长度:"..#tab) --输出 5
print("长度:"..table.getn(tab)) --输出 5
print("长度:"..table.maxn(tab)) --输出 5
tab = {"a","b","c","d",'e'}
print("长度:"..#tab) --输出 0
print("长度:"..table.getn(tab)) --输出 0
print("长度:"..table.maxn(tab)) --输出 0
表的连接:
tab = {"a","b","c","d",'e'}--表的连接 仅针对数组
str = table.concat(tab)
print(str) --输出 abcde
str = table.concat(tab,"-")
print(str) --输出 a-b-c-d-e
str = table.concat(tab,"+",2,4)
print(str) --输出 b+c+d
print("长度:"..#tab) --输出 长度:5
表中插入数据
tab = {"a","b","c","d",'e'}
table.insert(tab,"f") -- 末尾插入
print(tab[#tab])
table.insert(tab,2,"f") -- 指定索引位插入
print(tab[2])
print(tab[3])
print("长度:"..#tab)
表中移除数据
tab = {"a","b","c","d",'e'}
table.remove(tab,2) -- 移除指定索引位数据
print(tab[2]) --输出 c
print("长度:"..#tab) --输出 长度:4
表排序
按照Asiik码排序
tab = {"a",'e',"b","c","d"}
print(table.concat(tab,"-")) --输出 a-e-b-c-d
table.sort(tab)
print(table.concat(tab,"-")) --输出 a-b-c-d-e
tab = {"2",'1',"5","3","4"}
table.sort(tab)
print(table.concat(tab,"-")) --输出 1-2-3-4-5
设置元表 setmetatable
setmetatable 设置表的元表,返回值是当前表
getmetatable 获取表的元表,返回值是当前表的元表
tab = {'a','b','c',"d"}
metatable = {}
t = setmetatable(tab,metatable) --
print(tab) --输出 table: 0026BCE8
print(t) --输出 table: 0026BCE8
print(tab[1]) --输出 a
print(t[1]) --输出 a
g = getmetatable(tab)
print(metatable) --输出 table: 0026BC98
print(g) --输出 table: 0026BC98
设置元表 __metatable
setmetatable(table,metatable): 对指定 table 设置元表(metatable),如果元表(metatable)中存在 __metatable 键值,setmetatable 会失败。
getmetatable(table): 返回对象的元表(metatable)。
mytable = setmetatable({1,2,3},{__metatable = "禁止访问 保护元表"})
print(mytable) --输出 table: 0026BB80
print(getmetatable(mytable)) --输出 禁止访问 保护元表
设置元表 __index
这是 metatable 最常用的键。
当你通过键来访问 table 的时候,如果这个键没有值,那么Lua就会寻找该table的metatable(假定有metatable)中的__index 键。如果__index包含一个表格,Lua会在表格中查找相应的键。
-- __index = 匿名函数
tab = {'a','b','c',"d"}
metatable = { __index = function(mytable, k)
print("K: "..k)
end}
setmetatable(tab,metatable)
print(tab[1]) --输出 a
print(tab[9]) --输出 K: 9 在输出 nil
tab = {'a','b','c',"d"}
metatable = { __index = function(mytable, k)
return "萌萌茶"
end}
setmetatable(tab,metatable)
print(tab[1]) --输出 a
print(tab[9]) --输出 萌萌茶
print(tab.k1) --输出 萌萌茶
-- __index = 表
newtable = { }
newtable[1] = "10"
newtable[8] = "80"
tab = {'a','b','c',"d"}
metatable = { __index = newtable}
t = setmetatable(tab , metatable)
print(tab[1]) --输出 a
print(tab[8]) --输出 80
print(tab[9]) --输出 nil
设置元表 __newindex
__newindex 元方法用来对表更新,__index则用来对表访问 。
当你给表的一个缺少的索引赋值,解释器就会查找__newindex 元方法:如果存在则调用这个函数而不进行赋值操作。
-- __newindex = 匿名函数
tab = {'a','b','c',"d"}
metatable = { __newindex = function(mytable,k,v)
print("K: "..k.." v: "..v)
end}
setmetatable(tab,metatable)
tab[5] = 5
print(tab[5]) --输出 K: K: 5 v: 5 在输出 nil
-- rawset 更新表的键值对
tab = {'a','b','c',"d"}
metatable = { __newindex = function(mtable,k,v)
rawset(mtable,k,v)
end}
setmetatable(tab,metatable)
tab[5] = 5
print(tab[5]) --输出 5
-- __newindex = 表
-- __newindex 后面可以跟上表, 但是添加键值对时 是添加到了表newtable中
newtable = { }
tab = {'a','b','c',"d"}
metatable = { __newindex = newtable}
setmetatable(tab,metatable)
tab[5] = 5
print(tab[5]) --输出 nil
print(newtable[5]) --输出 5
设置元表 __add相关
__add 对应的运算符 '+'.
__sub 对应的运算符 '-'.
__mul 对应的运算符 '*'.
__div 对应的运算符 '/'.
__mod 对应的运算符 '%'.
__unm 对应的运算符 '-'.
__concat 对应的运算符 '..'.
__eq 对应的运算符 '=='.
__lt 对应的运算符 '<'.
__le 对应的运算符 '<='.
--__add
newtable = { "e",'f'}
tab = {'a','b','c',"d"}
metatable = { __add = function(a,b)
local x = {}
for k,v in pairs(a) do
table.insert(x,v)
end
for k,v in pairs(b) do
table.insert(x,v)
end
return x
end}
setmetatable(tab,metatable)
y = table.concat(tab+newtable)
print(table.concat(tab)) --输出 abcd
print(y) --输出 abcdef
设置元表 __call
__call 元方法在 Lua 调用一个值时调用
tab = {'a','b','c',"d"}
metatable = { __call= function(tab,arg)
return "萌萌茶"
end}
setmetatable(tab,metatable)
print(tab()) --输出 萌萌茶
设置元表 __tostring
__tostring 元方法用于修改表的输出行为
tab = {'a','b','c',"d"}
metatable = { __tostring= function(tab)
return "萌萌茶"
end}
print(setmetatable(tab,metatable)) --输出 萌萌茶
表 设置 面向对象
tab = {k1 = "a", k2 = 2}
tab.eat = function(x,self)
print("测试:"..x..self.k1)
end
self = tab
tab.eat(2,self) --输出测试:2a
print("\nYC ————————————> 13")
tab = {'a','b','c',"d"}
function tab:eat()
print("测试:"..self[1])
end
tab:eat() --输出 测试:a
tab:eat(1) --输出 测试:a
表面向对象 设置 构造函数
tab = {'a','b','c',"d"}
function tab:new(x)
local t = x or {}
setmetatable(t,{__index = self})
return t
end
t1 = tab:new()
print(t1[1]) --输出 a
t1[5] = 5
print(t1[5]) --输出 5
print(tab[5]) --输出 nil
表面向对象 设置 多参数构造函数
tab = {'a','b','c',"d"}
function tab:new(x,y,z)
local t = z or {}
setmetatable(t,{__index = self})
print(x..y)
return t
end
t2 = tab:new("2",1,{"e"}) --输出 21
print(t2[1]) --输出 e
print(t2[2]) --输出 nil
print(t2[5]) --输出 nil
t3 = tab:new("3",1)
print(t3[1]) --输出 nil
print(t3[2]) --输出 nil
print(t3[5]) --输出 nil
表面向对象其他写法与继承
tab = {'a','b','c',"d"}
function tab:new(x,y,z)
local t = z or {}
setmetatable(t,self)
self.__index = self
print("xy: "..x..y)
return t
end
newtab = tab:new(1,2) --输出 xy: 12
newtab.c = "萌萌茶"
print(newtab.c) --输出 萌萌茶
--继承
n = newtab:new(2,3) --输出 xy: 23
print(n.c) --输出 萌萌茶