lua metatables中操作__index

本文详细介绍了Lua中__index元方法的使用方法及其应用场景。通过两种不同的实现方式(函数和表),展示了如何利用__index实现面向对象的查找操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

reference:

http://www.lua.org/manual/5.3/manual.html

__indexThe indexing access table[key]. This event happens when table is not a table or when key is not present in table. The metamethod is looked up in table.

Despite the name, the metamethod for this event can be either a function or a table. If it is a function, it is called with table and key as arguments, and the result of the call (adjusted to one value) is the result of the operation. If it is a table, the final result is the result of indexing this table with key. (This indexing is regular, not raw, and therefore can trigger another metamethod.)

 

__index这个重载,主要是重载了find key的操作,这操作可以让Lua变得有点面向对象的感觉。 所谓__index,说得明确一点,如果我们有两个对象a和b,我们想让b作为a的prototype只需要:

setmetatable(a, {__index = b})

 

metamethod 为function

示例:

Window_Prototype = {x=0, y=0, width=100, height=100}
MyWin = {title="Hello"}
Window = {}
Window.__index = function(table, key)
  return Window_Prototype[key]
end
setmetatable(MyWin,Window)
print(MyWin.title, MyWin.width)

执行结果:

Hello	100

metamethod 为table

Window_Prototype = {x=0, y=0, width=100, height=100}
MyWin = {title="Hello"}
Window = {}
Window.__index = Window_Prototype
setmetatable(MyWin,Window)

print(MyWin.title, MyWin.width)

执行结果:

Hello	100

metamethod 为table简洁的书写方式:

Window_Prototype = {x=0, y=0, width=100, height=100}
MyWin = {title="Hello"}
setmetatable(MyWin, {__index = Window_Prototype})

print(MyWin.title, MyWin.width)

 

转载于:https://my.oschina.net/u/2326611/blog/840975

### 关于云风的 Lua 项目、教程和代码 #### CloudLua 面向对象编程 云风在其博客中分享了一种实现 Lua 中面向对象编程的方法[^4]。这种方法通过元表(metatables)和闭包实现了类继承机制。以下是其实现的核心逻辑: ```lua local _class = {} function class(super) local class_type = {} class_type.ctor = false class_type.super = super -- 创建新实例 class_type.new = function(...) local obj = {} local create create = function(c, ...) if c.super then create(c.super, ...) -- 调用父类构造器 end if c.ctor then c.ctor(obj, ...) -- 调用当前类构造器 end end create(class_type, ...) setmetatable(obj, { __index = _class[class_type] }) return obj end local vtbl = {} _class[class_type] = vtbl -- 设置元表,支持方法覆盖和继承 setmetatable(class_type, { __newindex = function(t, k, v) vtbl[k] = v end }) if super then setmetatable(vtbl, { __index = function(t, k) local ret = _class[super][k] vtbl[k] = ret return ret end }) end return class_type end ``` 此代码片段展示了如何创建带有继承功能的 Lua 类型结构。 --- #### Unity3D 中嵌入 Lua 的方案 云风还讨论了在 Unity3D 的 Mono 虚拟机中嵌入 Lua 的一种方式[^1]。具体来说,`SharpLua` 是一个用于初始化 Lua 环境的关键模块。开发者可以通过加载指定的第一个 Lua 文件来启动虚拟机环境,并调用 `require "sharplua"` 来完成必要的初始化工作。该模块不仅提供了底层的数据交换接口,还封装了一些便于从 Lua 层面回调 C# 方法的功能 API。 --- #### Lua 垃圾回收机制分析 关于 Lua 的垃圾回收机制,云风深入探讨了其中的一些细节[^3]。特别是对于 closed upvalue 的处理,在 `luaF_close` 函数中会将其移除出线程链表并重新链接到其他 GC 对象链中。这一过程涉及多个核心函数的操作,例如 `unlinkupval` 和 `setobj`,以及最终通过 `luaC_linkupval` 实现的重链接操作。 --- #### LuaECS —— 数据驱动设计 另一个值得关注的内容是由云风推荐的一个轻量级 Lua 实体组件系统(Entity Component System, ECS)。该项目允许用户轻松定义组件并通过简单的方式注册它们至实体中[^2]。此外,它还支持直接从 C/C++ 一侧访问这些组件,这得益于 `luaecs.h` 提供的一系列便捷 API。 --- #### 总结 以上内容涵盖了云风所参与或推荐的部分 Lua 技术领域成果,包括但不限于面向对象扩展、Unity 插件开发实践、GC 源码解读以及基于数据驱动的游戏框架构建等方面的知识点。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值