lua metatable 和 _index

中文博客解释:

http://www.cnblogs.com/simonw/archive/2007/01/17/622032.html

metatable:http://www.lua.org/pil/13.html

表的通用方法中,有些缺失的, 使用metatable可以定义表的这些默认方法:

add, sub, mul, div, mod, pow, unm, concat, len, eq, lt, le, tostring, gc, index, newindex, call...

__index: http://www.lua.org/pil/13.4.1.html

当访问表中无此元素, 则查询metatable中是否有__index, 如果有则获取_index调用结果, 如果没有__index则返回nil

__newindex : http://www.lua.org/pil/13.4.2.html

  当设置表中不存在的key时候, 触发调用metatable 的 __newindex, 如果没有__newindex则设置到目标表的属性上, 如果存在__newindex, 则执行它。

例子:

__index __newindex 对比:

local Bird = {CanFly = true}

function Bird:New()
local b = {}
setmetatable(b, self)
self.__index = self
--self.__newindex = self
return b
end local Ostrich = Bird:New() --Bird.CanFly is true, Ostrich.CanFly is true
Ostrich.CanFly = false --Bird.CanFly is true, Ostrich.CanFly is false print("Ostrich.CanFly="..tostring(Ostrich.CanFly))
print("Bird.CanFly="..tostring(Bird.CanFly))

其他测试实验:

--定义2个表
a = {, }
b = {, }
--用c来做Metatable
c = {}
--重定义加法操作
c.__add = function(op1, op2)
for _, item in ipairs(op2) do
table.insert(op1, item)
end
return op1
end
--自定义方法
c.print = function()
print("c print!");
end
--将a的Metatable设置为c, 报错,print为nil NOK
--[[
setmetatable(a, c)
a.print()
]]
--将a的Metatable设置为c, 调用c定义的内置函数 OK
--d现在的样子是{5,6,7,8}
--[[
setmetatable(a, c)
d = a + b
for _, item in ipairs(d) do
print(item)
end
]]
--将a的__index设置为c, 可以调用c定义的print OK
--[[
setmetatable(a, {__index = c})
a.print()
]]
--将a的metatable设置为c, 可以调用c定义的print OK
--[[
c.__index = c
setmetatable(a, c)
a.print()
--]]
--将a的__index不能直接复制为c, 报错, NOK
--[[
a.__index = c
a.print()
--]]
--将a的__index设置为c, 报错a不能执行算出运算, NOK
--[[
setmetatable(a, {__index = c})
d = a + b
for _, item in ipairs(d) do
print(item)
end
]]
--将a的__add设置为c的__add, OK
--[[
setmetatable(a, {__add=c.__add})
d = a + b
for _, item in ipairs(d) do
print(item)
end
--]]
-- 可以调用自定义方法, 和 内置方法 OK
--[[
setmetatable(a, {__index = c, __add=c.__add})
a.print()
d = a + b
for _, item in ipairs(d) do
print(item)
end
--]]

lua metatable 和 _index 实验的更多相关文章

  1. Lua metatable & metamethod

    [Lua metatable & metamethod] Lua中的每个值都有一套预定义的操作集合,比如数字是可以相加的,字符串是可以连接的,但是对于两个table类型,则不能直接进行“+”操 ...

  2. lua metatable(元表)

    lua metatable (元表) 概述 普通 table 类型的表仅能够做一些单表操作, 无法进行一些特殊的以及两个表的操作, 比如 table1 + table2, print(table3) ...

  3. lua metatable和metamethod元表和元方法

    Lua中提供的元表是用于帮助Lua数据变量完成某些非预定义功能的个性化行为,如两个table的相加.假设a和b都是table,通过元表可以定义如何计算表达式a+b.当Lua试图将两个table相加时, ...

  4. lua面向对象封装及元表(metatable)性能测试

    Lua本身是没有面向对象支持的,但面向对象编程在逻辑复杂的大型工程却很有用.于是很多人用Lua本身的数据结构table来模拟面向对象.最简单的一种方法是把对象的方法.成员都放到table中.如: -- ...

  5. lua表类型

    Lua的表的定义: typedef struct Table { CommonHeader; lu_byte flags; lu_byte lsizenode; /* log2 of size of ...

  6. 基于Lua插件化的Pcap流量监听代理

    1.前言 我们在实际工作中,遇到了一个这样的用例,在每天例行扫描活动中,发现有些应用系统不定期的被扫挂,因为我们不是服务的制造者,没有办法在不同的系统里打印日志,所以我们就想用一个工具来获取特定服务的 ...

  7. elastic search使用总结

    1. elasticsearch安装 官方下载地址:https://www.elastic.co/downloads/elasticsearch 解压文件 elasticsearch-2.4.0.zi ...

  8. lua-C++ userdata使用

    lua-C++ userdata使用 所负责的产品使用非常灵活,可设置的参数上千个,而且还支持用户用lua进行流程控制,所以开发中要用到很多lua.C++混合编程.之前对这些也还是一知半解,只会依葫芦 ...

  9. ROS学习笔记1-引言

    该学习笔记参考ROS官方wiki的内容,见:http://wiki.ros.org/ROS/Introduction 什么是ROSROS的全称是Robot Operating System,即机器人操 ...

随机推荐

  1. 在 MySQL 中查找含有目标字段的表

    要查询数据库中哪些表含有目标字段,可以使用语句: SELECT TABLE_SCHEMA,TABLE_NAME FROM information_schema.`COLUMNS` WHERE COLU ...

  2. Bootstrap页面布局19 - BS提示信息

    提示信息的设计 提示信息的类: .alert:提示信息类 .alert alert-danger: .alert alert-error: .alert alert-success: .alert a ...

  3. Bootstrap页面布局7 - Bootstrap响应式布局的实用类

    在bootstrap-responsive.css这个CSS样式表中已经为我们设定好了几个实用的类: .visible-phone: 在智能手机设备上显示这个元素,在其他设备上隐藏该元素 .visib ...

  4. easy ui 问题

    easyui  的样式  和Bootstrap css   有冲突,不要一起使用 日期禁止输入 editable="false" ------------------------- ...

  5. 耦合 Coupling the object-oriented paradigm && data coupling

    Computer Science An Overview _J. Glenn Brookshear _11th Edition 耦 两个人一起耕地 one of the benefits of the ...

  6. P1379 八数码问题

    aoapc上的八数码问题,在luogu上也有类似的题,p1379,经典题目,lrj给出了一个算法,同时给出了三种判重的方法.本来想用std::queue改写一下,但是出了各种问题,只好抄代码ac掉这道 ...

  7. Flink - Juggling with Bits and Bytes

    http://www.36dsj.com/archives/33650 http://flink.apache.org/news/2015/05/11/Juggling-with-Bits-and-B ...

  8. svn 入门

    SVN版本:1.5 及更新版本 名词说明: WC:Working Copy 你的工作区 Versioned:受控的:受版本控制的 SVN是什么? SVN是开源的版本控制系统. 比CVS更多的特性.一个 ...

  9. 【ZBar】ios错误ignoring file xxx missing required architecture x86_64 in file

    解决方法: 1.在Project target里"Architectures"设置为:Standard (armv7,armv7s)或者  Standard (armv7,arm6 ...

  10. 添加Ubuntu的库文件路径

    添加Ubuntu的库文件路径 http://blog.csdn.net/r91987/article/details/6879062 关于ubuntu添加共享库路径: 1. 将绝对路径写入 /etc/ ...