LUA Metatables
__index:当我们访问一个表中的元素不存在时,则会触发去寻找__index元方法,如果不存在,则返回nil,如果存在,则返回结果。
博主注:__index有点像异常处理的意思
__newindex: 当给你的表中不存在的值进行赋值时,lua解释器则会寻找__newindex元方法,发现存在该方法,则执行该方法进行赋值,注意,是使用rawset来进行赋值,至于原因,后面会讲到。
rawget: 是为了绕过__index而出现的,直接点,就是让__index方法的重写无效,可以理解为单纯的获取本表而不关联到父表(元表)(我这里用到"重写"二字,可能不太对,希望能得到纠正)
rawset: 设置表[索引]值,真正的值没有调用任何元方法。表必须是一个表,索引从不同的无任何值,Lua的任何值。这个函数返回表
__index
This is a very commonly used and versatile metamethod, it lets you run a custom function or use a "fallback" table if a key in a table doesn't exist. If a function is used, its first parameter will be the table that the lookup failed on, and the second parameter will be the key. If a fallback table is used, remember that it can trigger an __index metamethod on it if it has one, so you can create long chains of fallback tables. local func_example = setmetatable({}, {__index = function (t, k) -- {} an empty table, and after the comma, a custom function failsafe
return "key doesn't exist"
end}) local fallback_tbl = setmetatable({ -- some keys and values present, together with a fallback failsafe
foo = "bar",
[123] = 456,
}, {__index=func_example}) local fallback_example = setmetatable({}, {__index=fallback_tbl}) -- {} again an empty table, but this time with a fallback failsafe print(func_example[1]) --> key doesn't exist
print(fallback_example.foo) --> bar
print(fallback_example[123]) --> 456
print(fallback_example[456]) --> key doesn't exist
http://lua-users.org/wiki/MetamethodsTutorial
LUA Metatables的更多相关文章
- Lua基础之MetaTable(6)
Lua基础之MetaTable(6) 转载地址:http://nova-fusion.com/2011/06/30/lua-metatables-tutorial/ 关于MetaTable的补充:ht ...
- Lua语言中文手册 转载自网络
Programming in LuaCopyright ® 2005, Translation Team, www.luachina.net Programming in LuaProgramming ...
- Lua高级教程Metatables
什么是Metatable metatable是Lua中的重要概念,每一个table都可以加上metatable,以改变相应的table的行为. Metatables举例 -- 声明一个正常的关系变量 ...
- [Lua]Lua高级教程Metatables
什么是Metatable metatable是Lua中的重要概念,每一个table都可以加上metatable,以改变相应的table的行为. Metatables举例 -- 声明一个正常的关系变量 ...
- lua的Metatables和Metamethods
Metatable: lua中的每一个表都有其Metatable,默认情况下Metatable为nil.可通过setmetatable函数设置或者改变一个表的Metatable, 也可以通过getme ...
- 15分钟学会Lua
lua的很多语法跟matlab很像 最基本的赋值是一样的 循环和选择判断后面必须跟一个关键字:do和then ,, do ... end if - then - end table是lua的唯一一种数 ...
- Lua 与 Redis
Lua 与 Redis 标签: Java与NoSQL 从 2.6版本 起, Redis 开始支持 Lua 脚本 让开发者自己扩展 Redis - 案例-实现访问频率限制: 实现访问者 $ip 在一定的 ...
- Lua: 好的, 坏的, 和坑爹的
好的 小巧: 20000行C代码 可以编译进182K的可执行文件 (Linux下). 可移植: 只要是有ANSI ...
- C/C++ Lua Parsing Engine
catalog . Lua语言简介 . 使用 Lua 编写可嵌入式脚本 . VS2010编译Lua . 嵌入和扩展: C/C++中执行Lua脚本 . 将C++函数导出到Lua引擎中: 在Lua脚本中执 ...
随机推荐
- Beta阶段第1周/共2周 Scrum立会报告+燃尽图 01
作业要求[https://edu.cnblogs.com/campus/nenu/2018fall/homework/2383] 版本控制:https://git.coding.net/liuyy08 ...
- ubuntu:在ubuntu上安装vmware12
在ubuntu上安装vmware12 下载vmware12 https://pan.baidu.com/s/1i5BQEmL 官方的 密匙 5A02H-AU243-TZJ49-GTC7K-3C ...
- Python流程控制-while循环-for循环
写重复代码 是可耻的行为 -------------- 完美的分割线 -------------- 摘录自:http://www.runoob.com/python/python-loops.htm ...
- Centos7 服务 service 设置命令 systemctl 用法 (替代service 和 chkconfig)
在Centos 中 systemctl 是设置系统服务的命令,即 service , 它融合之前service和chkconfig的功能于一体. 可以使用它永久性或只在当前会话中启用/禁用服务 ...
- 6-8 Percolate Up and Down(20 分)
Write the routines to do a "percolate up" and a "percolate down" in a binary min ...
- 51nod 算法马拉松4 D装盒子(网络流 / 二分图最优匹配)
装盒子 基准时间限制:1 秒 空间限制:131072 KB 分值: 160 有n个长方形盒子,第i个长度为Li,宽度为Wi,我们需要把他们套放.注意一个盒子只可以套入长和宽分别不小于它的盒子,并且 ...
- streamsets origin 说明
origin 是streamsets pipeline的soure 入口,只能应用一个origin 在pipeline中, 对于运行在不同执行模式的pipeline 可以应用不同的origin 独立模 ...
- TortoiseSVN 结合使用哪个问题跟踪系统比较好?TRAC?REDMINE?都有什么优缺点?
TortoiseSVN 结合使用哪个问题跟踪系统比较好?TRAC?REDMINE?都有什么优缺点? -------------------------------------------------- ...
- 基于 jmeter 和 shell 的接口性能自动化
jmeter+shell 1. 总体需求 由于性能测试中涉及的查询接口多,版本迭代频繁,版本更新后自动跑一轮查询业务的性能,可以及时发现一些开发修复bug触发的非预期的bug,利用晚上时间快速重测性能 ...
- 【python】 Windows下pip安装包报错:Microsoft Visual C++ 9.0 is required Unable to find vcvarsall.bat
刚在机器上windows环境下装上pip方便以后安装包的时候使用,谁知道第一次使用pip安装asyncio的时候就报错. 在Windows7x64下使用pip安装包的时候提示报错:Microsoft ...