
文章目录
一、简介
元表(metadata)是Lua中独有的概念,在实际项目中的使用非常广泛。
元表的表现行为类似于C++语言中的操作符重载。比如可以重载表中的__add函数,来计算两个表Lua数组的并集;或者重载__tostring,来定义转换为字符串的函数。
Lua语言中提供两个处理元表的函数:
- 第一个是setmetatable(table,metatable),用于为一个table设置元表
- 第二个是getmetadata(table),用于获取table的元表
设置元表的方法:
方法一:
local mytable = {}
local mymetatable = {}
setmetatable(mytable, mymetatable)
方法二:
local mytable = setmetatable({}, {})
二、Lua中可以重载的函数
2.1 操作符重载
| 元方法 | 含义 |
| “__add” | +操作 |
| “__sub” | -操作 |
| "__mul" | *操作 |
| "__div" | /操作 |
| "__mod" | %操作 |
| "__pow" | ^操作 |
| "__num" | 一元-操作 |
| "__concat" | ..字符串连接操作 |
| "__len" | #操作 |
| "__eq" | ==操作 |
| "__lt" | <操作|
| "__le"| <= 操作 |
2.2 其他
| 元方法 | 含义 |
| "__index" | 取下标操作用于访问table[key]|
| "__newindex" | 赋值给指定下标table[key] = value |
| "__tostring" | 转换成字符串 |
| "__call" | 当Lua调用一个值时调用 |
| "__mode" | 用于弱表(week table) |
| "__metatable" | 用于保护metatable不被访问 |
三、栗子
3.1 重载__add元方法来计算集合的并集实例
通过重载 “__add” 元方法来计算集合的并集实例:
local set1 = {10, 20, 30} -- 集合
local set2 = {20, 40, 50} -- 集合
-- 将用于重载__add的函数,注意第一个参数是self
local union = function (self, another)
local set = {}
local result = {}
-- 利用数组来确保集合的互异性
for i, j in pairs(self) do set[j] = true end
for i, j in pairs(another) do set[j] = true end
-- 加入结果集合
for i, j in pairs(set) do table.insert(result, i) end
return result
end
setmetatable(set1, {__add = union}) -- 重载 set1 表的 __add 元方法
local set3 = set1 + set2
for _, j in pairs(set3) do
io.write(j.." ") -->output:30 50 20 40 10
end
3.2 __index 元方法重载
实现了在表中查找键不存在时转而在元表中查找该键的功能:
mytable = setmetatable({key1 = "value1"}, --原始表
{__index = function(self, key) --重载函数
if key == "key2" then
return "metatablevalue"
end
end
})
print(mytable.key1,mytable.key2) --> output:value1 metatablevalue
3.3 __tostring 元方法重载
与 Java 中的 toString() 函数类似,可以实现自定义的字符串转换。
arr = {1, 2, 3, 4}
arr = setmetatable(arr, {__tostring = function (self)
local result = '{'
local sep = ''
for _, i in pairs(self) do
result = result ..sep .. i
sep = ', '
end
result = result .. '}'
return result
end})
print(arr) --> {1, 2, 3, 4}
3.4 __call 元方法重载
__call 元方法的功能类似于 C++ 中的仿函数,使得普通的表也可以被调用。
functor = {}
function func1(self, arg)
print ("called from", arg)
end
setmetatable(functor, {__call = func1})
functor("functor") --> called from functor
print(functor) --> output:0x00076fc8 (后面这串数字可能不一样)
3.5 __metatable 元方法
假如我们想保护我们的对象使其使用者既看不到也不能修改 metatables。我们可以对 metatable 设置了 __metatable 的值,getmetatable 将返回这个域的值,而调用 setmetatable 将会出错:
Object = setmetatable({}, {__metatable = "You cannot access here"})
print(getmetatable(Object)) --> You cannot access here
setmetatable(Object, {}) --> 引发编译器报错