Lua 笔记 1-1 基本函数使用
来源:互联网 发布:html调用js函数 编辑:程序博客网 时间:2024/06/05 04:43
参考资料:
Lua中的元表和元方法
http://www.cnblogs.com/whiteyun/archive/2009/09/01/1558147.html
Lua中的基本函数库
http://www.cnblogs.com/whiteyun/archive/2009/08/12/1543184.html
--------------------------------------------------------------------------------
Lua比较重要的点(一)
setmetatable用法
setmetatable(table, metatable)
功能:为指定的table设置元表metatable,如果metatable为nil则取消table的元表,当metatable有__metatable字段时,将触发错误
注:只能为LUA_TTABLE 表类型指定元表参数:
table:表
metatable:元表
结合__index, __newindex理解
__index:
元表里最常用的索引可能是__index,它可以包含表或函数。
当你通过索引来访问表, 不管它是什么(例如t[4], t.foo, 和t["foo"]), 以及并没有分配索引的值时,Lua 会先在查找已有的索引,接着查找表的metatable里。
(如果它有)查找__index 索引。 如果__index 包含了表, Lua会在__index包含的表里查找索引。
(我的理解是如果table没有的话就会找有没有元表,如果没有就是空,如果有的话就看看索引__index,如果没有的话是空,
有的话就是看看元表里面__index指向的对象里面有没有,没有就返回空,有就返回相应的地方。)
简单说就是先找自己,再找元表的索引(即元表的__index开始找)。
e.g
-- table BtableB = { func1,}tableB.func1 = function(x, y) print(x + y)end-- table CtableC= { func1,}tableC.func1 = function(x, y) print("yeah")end-- table AtableA = { x = 3, y = 4,}-- 创建tableA的元表tableBtableA = setmetatable(tableA, tableB)-- 把tableB的索引保存自己tableB.__index = tableCprint(getmetatable(tableA))print(tableA)print(tableB)tableA.func1(tableA.x, tableA.y)tableB.func1(3, 5)
输出:
table: 000000000CB7DA00
table: 000000000CB7D910
table: 000000000CB7DA00
yeah
8
---------------------------------------------------------------------------------------------------
__newindex:
下一个是__newindex, 它和__index类似。
和 __index一样,它可以包含函数和表。当你给表中不存在的值赋值时,Lua会在metatable里查找__newindex,调用顺序和 __index一样。
如果__newindex是表,索引和值会设置到指定的表.
简单说就从创建元表开始,直接对创建table的元素做操作返回到元表上,原来table不操作。
e.g
-- tableB 都定义了tableB = { x1 = 10, func1,}tableB.func1 = function() print("yeah")end-- B第一次打印print(tableB.x1)tableB.func1()-- 建立元表关系tableA = setmetatable({}, tableB)tableB.__newindex = tableBtableA.x1 = 20-- B第二次打印print(tableB.x1)tableB.func1()-- A第一次打印print(tableA.x1)print(tableA.func1)
输出:
10yeah
20
yeah
nil
nil
--------------------------------------------------------------------------------
暂时工作比较常用(一)
assert用法
assert (condition, str)功能:相当于C的断言,
参数:
condition:当表达式condition为nil或false将触发错误,
str:发生错误时返回的信息,默认为"assertion failed!"
e.g
assert(nil, "sdk")
直接终断程序
--------------------------------------------------------------------------------
print用法
print(...)
功能:简单的以tostring方式格式化输出参数的内容
e.g
print("start" .. " studying.")输出:
start studying.
--------------------------------------------------------------------------------
tonumber用法
tonumber(v, radix)
功能:尝试将参数e转换为数字,当不能转换时返回nil
radix(2~36)指出参数e当前使用的进制,默认为10进制
一般用途是把字符串转换成数字,方便计算
e.g
-- 11 是数值,2 是进制tonumber("11",2)print(tonumber("11",2))print(tonumber(11,2))
输出:
3
3
--------------------------------------------------------------------------------
tostring用法
tostring(str)
功能:将参数e转换为字符串,此函数将会触发元表的__tostring事件
一般用途是把数字转换成字符串
e.g
print(tostring("1234"))
输出:
1234
--------------------------------------------------------------------------------
type用法
type(var)
功能:返回参数的类型名("nil","number","string", "boolean", "table","function", "thread", "userdata")
e.g
print(type("start"))
输出:
string
--------------------------------------------------------------------------------
暂时工作比较少用到(二)
--------------------------------------------------------------------------------
pcall用法
pcall(func, ...)
参数:
func:函数
...:不定的参数
功能:在保护模式下调用函数(即发生的错误将不会反射给调用者)
当调用函数成功能返回true,失败时将返回false加错误信息
e.g
function printFunc(str1, str2, str3) print(str1, str2, str3)endx = pcall(printFunc, "string1","string2")print(x)
输出:
string1 string2 nil
true
--------------------------------------------------------------------------------
xpcall用法
xpcall(func, errorFunc)
参数:
func:函数
errorFunc:错误处理的函数
功能:与pcall类似,在保护模式下调用函数(即发生的错误将不会反射给调用者)
但可指定一个新的错误处理函数句柄
当调用函数成功能返回true,失败时将返回false加err返回的结果
e.g
function printFunc(str1, str2, str3) print(str1, str2,str3)endfunction errorFunc() print("error")endx, msg = xpcall(nilFunc, errorfunc)print(x, msg)
输出:
error
false nil
x, msg = xpcall(nilFunc, nil)print(x, msg)
输出:
false error in errorhandling
--------------------------------------------------------------------------------
- Lua 笔记 1-1 基本函数使用
- lua笔记之函数(1)
- Lua学习笔记1--基本变量类型
- lua基本知识点1
- Lua笔记1-lua是什么
- lua之基本函数
- Lua 基本函数
- lua之基本函数
- lua之基本函数
- lua table 基本使用
- mysql学习笔记1--MySQL存储函数之基本常用函数的分类与使用
- Lua学习笔记(1)
- Lua学习笔记1
- lua学习笔记(1)
- lua自学笔记1
- 初学lua笔记1
- 【Lua】语法笔记#1
- lua笔记1
- 信息增益(IG,Information Gain)的理解和计算
- 14随谈
- 设计模式C++实现(3)——适配器模式
- 产品经理的自我修养:方法论
- Matlab虚拟现实工具箱——快速配置
- Lua 笔记 1-1 基本函数使用
- mssql查看数据库中所有触发器语句
- oracle实例恢复 详解理论知识
- javaEE-类绑定与类加载
- CSS Sprites的一些技巧
- c++构造函数再调用构造函数
- CSS雪碧:要还是不要?
- 工程文件存储策略,实现前后兼容打开。
- Word2vec在事件挖掘中的调研