Lua元表

作者: 沧州宁少 | 来源:发表于2018-04-09 19:22 被阅读0次

Lua元表

在Lua中,我们可以通过key找到对应的value值,但是无法对两个table进行操作。

在Lua中为我们提供了元表(Metatable),允许我们改变table的行为,每个行为关联了对应的元方法

  • setmetatable(table,metatable) 对指定的table设置元表。如果元表存在_metatable键值,则方法会失败。
  • getmetatable(table)得到对象的原表

为一个表设置其对应的原表

  • setmetatable(mytable,metatable) function返回mytable。即为哪个原始表设置的元表

  • getmetatable(mytable) function返回metatable

  • index元方法。当我们去一个表去查找一个字段的时候,如果存在则直接返回。如果不存在的话,则去其对应的元表中去寻找 __index key,如果对应的是一个表的话,则去这个index对应的表中去寻找。找到则返回

table  = {key=david}

othertable = {other=3}

setmetatable(table,{__index=othertable})

print(table.key);

print(table.other)
  • 去metatable去寻找__index对应的是不是一个function。如果是的话,则把表本身和key传入。

       nativetable = {key='david'}
       setmetatable(nativetable,{__index=function (table,key)
       if key  == 'key2' then    
            return '牛B' 
       else     
            return nil    
       end  
       end
       })
     print(nativetable.key)
     print(nativetable.key2)
    
  • 使用__newIndex的方式可以对传入未知的key保存到 newIndex对应的表中,不进行直接的赋值。可以通过 rawset 函数来更新表。

    mytable = setmetatable({key1 = "value1"}, {
      __newindex = function(mytable, key, value)
            rawset(mytable, key, "\""..value.."\"")
    
      end
    })
    
    mytable.key1 = "new value"
    mytable.key2 = 4
    
    print(mytable.key1,mytable.key2)
    
  • 使用__add的方式,使2个表相加

    -- 计算表中最大值,table.maxn在Lua5.2以上版本中已无法使用
    -- 自定义计算表中最大键值函数 table_maxn,即计算表的元素个数
    function table_maxn(t)
        local mn = 0
        for k, v in pairs(t) do
            if mn < k then
                mn = k
            end
        end
        return mn
    end
    
    -- 两表相加操作
    mytable = setmetatable({ 1, 2, 3 }, {
        __add = function(mytable, newtable)
            for i = 1, table_maxn(newtable) do
                table.insert(mytable, table_maxn(mytable)+1,newtable[i])
            end
            return mytable
        end
    })
    
    secondtable = {4,5,6}
    
    mytable = mytable + secondtable
    for k,v in ipairs(mytable) do
        print(k,v)
    end
    
  • __tostring 元方法用于修改表的输出行为

    mytable = setmetatable({ 10, 20, 30 }, {
      __tostring = function(mytable)
        sum = 0
        for k, v in pairs(mytable) do
            sum = sum + v
        end
        return "表所有元素的和为 " .. sum
      end
    })
    print(mytable)
    

相关文章

  • Lua中元表的学习

    Lua本身没有面向对象的思想,但是可以根据表、元表、元方法来靠近它 一、元表与元方法的概念Lua中每个值都可具有元...

  • Lua 元表(Metatable)

    学习网站Lua 元表(Metatable)

  • Lua 实现面向对象 (原创)

    要理解Lua是如何实现面向对象的。首先要熟悉Lua元表的相关知识,可以阅读我上一篇文章《Lua元表 (Metata...

  • Lua学习

    Lua 学习 元表 setmetatable(table,metatable): 对指定table设置元表(met...

  • 2017.5.26

    lua学习:metatable 元方法,元表 lua 中的任何一个值都有其预定义的一套操作,这些操作都是在元表中定...

  • Lua元表

    Lua元表 在Lua中,我们可以通过key找到对应的value值,但是无法对两个table进行操作。 在Lua中为...

  • Lua元表

    @date: 2018-3-18 在Lua5.1语言中,元表 (metatable) 的表现行为类似于 C++ 语...

  • lua元表

    1、Lua 元表(Metatable) setmetatable(table,metatable): 对指定tab...

  • Lua 元表

    有一篇博客对Lua元表的介绍非常易懂,推荐阅读原博主的文章 点我前往 以下内容仅仅是个人为了加深记忆将原博主内容消...

  • 学习常用链接

    //Lua Lua table详解 Lua 元表详解 云风博客 //Unity Unity知识点 栈和队列 Uni...

网友评论

      本文标题:Lua元表

      本文链接:https://www.haomeiwen.com/subject/nwxwhftx.html