美文网首页unity3D技术分享
lua元表,B设置为A的元表

lua元表,B设置为A的元表

作者: 好怕怕 | 来源:发表于2022-09-05 15:41 被阅读0次
local config = {a=1}                          -- 普通表
local propConfig = {b=2,c="元表的值"}                      -- 元表
setmetatable(config,{__index = propConfig}) 
print(config.a)
print(config.c)

https://c.runoob.com/compile/66/

image.png
local config = {a=1}       
local propConfig = {c=344}
local cfg = setmetatable(config,{__index = function(tab,key) 
         if propConfig[key] then
                return propConfig[key]
            else
                return "没有值啊!"
            end
        end}) 
print(cfg.a)
print(cfg.ca)

相关文章

  • lua元表,B设置为A的元表

    https://c.runoob.com/compile/66/[https://c.runoob.com/com...

  • Lua学习

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

  • Lua中元表的学习

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

  • Lua-元表

    元表Metatable Lua提供了元表,允许我们改变table的行为,每个行为关联了对应的元方法。 例如,使用元...

  • 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 元表(Metatable)

    学习网站Lua 元表(Metatable)

  • 2017.5.26

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

网友评论

    本文标题:lua元表,B设置为A的元表

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