LUA 元表与元方法

来源:互联网 发布:红警2 mac os10.12 编辑:程序博客网 时间:2024/05/02 02:55
元表操作:
    setmetatable(table, metatable)
    getmetatable(table)
元方法操作:
    metatable.元方法 = function (可接受参数)
        (函数体)
    end
元方法:
    算数运算符:__add(加法)[+]、__mul(乘法)[*]、__sub(减法)[-]、__div(除法)[/]、__unm(相反数)[-]、__mod(取模)[%]、__pow(乘幂)[^]。
    逻辑运算符:__eq(等于)[=]、__lt(小于)[<]、__le(小于等于)[<=]。
    其他运算符:__concat(连接)[..]、__len(取长度)[#]。
    其他元方法: 
    __tostring:返回值(可接受参数:table)
    __call:函数调用(可接受参数:table, key)
    __metatable:保护元方法(字符串)
    __index:查找表索引(可接受参数:table, key)
    __newindex:添加新索引(可接受参数:table, key, value) 
原创粉丝点击