经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » 游戏设计 » 查看文章
Lua rawget rawset newindex 函数定义和例子
来源:cnblogs  作者:赵青青  时间:2018/11/29 9:07:01  对本文有异议

在绝大多数情况下,我们都不会用到rawget和rawset。

本文的运行环境:lua 5.3 for windows

rawset 赋值操作

rawset是在设置值的过程,进行处理,比如:当某个值改变时,触发事件。或修改某个key为新值。

来看看rawset函数的定义

  1. --- Sets the real value of `table[index]` to `value`, without invoking the
  2. --- `__newindex` metamethod. `table` must be a table, `index` any value
  3. --- different from **nil** and NaN, and `value` any Lua value.
  4. ---@param table table
  5. ---@param index any
  6. ---@param value any
  7. function rawset(table, index, value) end

看个例子,设置过__newindex之后,就不会调用__index了?

  1. local tb = {}
  2. setmetatable(tb, { __index = function()
  3. return "not find"
  4. end })
  5. setmetatable(tb, { __newindex = function(table, key, value)
  6. local patchKey = "version"
  7. if key == patchKey then
  8. rawset(table, patchKey, "补丁值")
  9. else
  10. rawset(table, key, value)
  11. end
  12. end })
  13. tb.version = "正常版本"
  14. tb.date = "2018"
  15. print(tb.version) --打印 补丁值
  16. print(tb.server) --打印nil,不会调用__index方法了?
  17. print(tb.date) --打印2018

经过我的测试后, 发现

  1. ---如果把__index放在__newindex之后,调用不存在值,才会调用__index方法
  2. 如果在__index__newindex之前,则不会调用

rawget 取原始值

rawget是为了绕过__index而出现的,直接点,就是让__index方法的重写无效

来看看rawget函数的定义

  1. --- Gets the real value of `table[index]`, the `__index` metamethod. `table`
  2. --- must be a table; `index` may be any value.
  3. ---@param table table
  4. ---@param index any
  5. ---@return any
  6. function rawget(table, index) end

编写一个例子,测试rawget绕过__index方法

  1. local tb = {}
  2. setmetatable(tb, { __index = function()
  3. return "not find"
  4. end })
  5. tb.version = "正常版本"
  6. print(tb.version)
  7. print(tb.server) ---不存在的值,调用__index方法
  8. --rawget是为了绕过__index而出现的,直接点,就是让__index方法的重写无效
  9. print(rawget(tb, "version")) --打印 正常版本
  10. print(rawget(tb, "server")) --打印nil

__newindex

__newindex可以和rawset配合使用,也可以单独使用

当为表分配值时,解释器会查找__newindex方法,如果存在,则解释器会调用它。

结合使用 __index和 __newindex,允许lua有强大的构造,从只读表,到具有默认值的表,到面向对象编程的继承

文档:https://www.lua.org/pil/13.4.2.html

Lua5.3 __index要通过setmetatable设置

在lua5.3中,直接使用tableA.__index = function() end 设置,我这边测试,并不会生效

  1. local tempTable = { memberB = "test" }
  2. tempTable.__index = function()
  3. return "not find"
  4. end
  5. print(tempTable.memberA) --打印 nil
  6. print(tempTable.memberB) --打印test

而通过这种方式就正常

  1. local tempTable = { memberB = "test" }
  2. ---__index定义了当key查找不到的行为
  3. setmetatable(tempTable, { __index = function()
  4. return "not find"
  5. end })
  6. print(tempTable.memberA) --打印 not find
  7. print(tempTable.memberB) --打印test
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号