lua基础篇(二)
来源:互联网 发布:网络电影怎么挣钱 编辑:程序博客网 时间:2024/05/29 19:43
元表
metatable:(元表)一组元操作(元方法)的集合;
__index:元方法之一,作用:定义了新的索引操作;
查找表的元方法,即metatable中的__index方法(也可以是另一个表)
原型:__index = fuunction(table,key) --此处的table为调用该元方法的
if type(key) == "number" then
print("the key's value is nil!");
end
end
__newindex:元方法之一,作用:定义新的赋值操作;
若对表中的key进行复制操作,执行该元方法(也可以是一个表);
原型:__newindex = function(table,key,value)
if key == "bucunzide" then
rawset(table,"bucunzide","geiyige");
end
end
--------------------------------------------------------------------------------------------
UIValueList = {name = "UIValueList"};
UIValueMet = {
--a = 100;
name = "UIValueMet";
__index = function ( table,k )
return rawget(UIValueMet,k);
end;
__newindex = function ( table,k,value ) //table,调用者;k,索引;value,值
-- body
if UIValueMet[k] == nil then
rawset(UIValueMet,k,value);
else
print("" .. value .. " ====");
end
end
};
setmetatable(UIValueList,UIValueMet);
UIValueMet.a = 100;
-- print(UIValueList.a);
-- print(UIValueMet.a);
UIValueList.b = 888;
print(UIValueList.b);
UIValueList.b = 123;
UIValueList.c = 888;
UIValueList.d = 888;
print(UIValueList.b);
print(UIValueList.c);
print(UIValueList.d);
-----------------------------------------------------------------------------------------
__add:元方法之一 ,作用类似于运算符重载,用于表之间的运算;
与__index、 __newindex不同,操作符索引只能是函数。它们接受的第一个参数总是目标表, 接着
是右值 (除了一元操作符"-",即索引__unm)。下面是操作符列表:
- __add: 加法(+)
- __sub: 减法(-)
- __mul: 乘法(*)
- __div: 除法(/)
- __mod: 取模(%)
- __unm: 取反(-), 一元操作符
- __concat: 连接(..)
- __eq: 等于(==)
- __lt: 小于(<)
- __le:小于等于(<=)
__mode:元方法之一,用于指定表的弱引用特性;
1)key值弱引用,只要其他地方没有对key值引用,那么,table自身的这个字段也会被删除。设置方法:setmetatable(t, {__mode = "k"});
2)value值弱引用,只要其他地方没有对value值引用,那么,table的这个value所在的字段也会被删除。设置方法:setmetatable(t, {__mode = "v"});
3)key和value弱引用,规则一样,但是key和value都同时生效,任意一个起作用时都会导致table的字段被删除。设置方法:setmetatable(t, {__mode = "kv"});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
t = {};
-- 给t设置一个元表,增加__mode元方法,赋值为"k"
setmetatable(t, {__mode = "k"});
-- 使用一个table作为t的key值
key1 = {name = "key1"};
t[key1] = 1;
key1 = nil;
-- 又使用一个table作为t的key值
key2 = {name = "key2"};
t[key2] = 1;
key2 = nil;
-- 强制进行一次垃圾收集
collectgarbage();
for key, value in pairs(t) do
print(key.name .. ":" .. value);
end
------------------------------------------------------------------------
rawset(table,key,value) :设置表的索引
rawget(table,key):获取表的索引
__index = fuunction(table,key)
if type(key) == "number" then
--table[key] = 1000; 若此处直接使用key索引会再次调用__index方法,进入死循环;
rawset(table,key,1000); 使用rawset()方法可以直接设置新的键值对,从而避免调用__index和__newindex方法;
end
end
-----------------------------------------------------------------------
lua的log输出:
log_enum = { zhandouxitong = 4,
rrwuyidyng = 5,
Wujiangxitong = 6,
Wujiangxitong2 = 7,
WujiangxitongServer = 8,
};
function Clog( str,type )
if type == log_enum.WujiangxitongServer then
local info = debug.getinfo(2,"nfSl");
local MSG = nil;
if info.name then
MSG = string.format("filed:<" .. info.source .. "|func: " .. info.name .. "|>; line:[" .. info.currentline .. "] MSG:\n\t" .. str);
else
MSG = string.format("filed:<" .. info.source .. "> line:[" .. info.currentline .. "] MSG:\n\t" .. str);
end
print(MSG);
end
end
关于debug.getinfo()
- lua基础篇(二)
- lua基础(二)
- Lua基础学习(二)
- Lua基础 函数(二)
- Lua基础 函数(二)
- Lua基础 函数(二)
- 二、lua语言基础2
- 【COCOS2DX-LUA 脚本开发之二】LUA语言基础
- 【COCOS2DX-LUA 脚本开发之二】LUA语言基础
- 【COCOS2DX-LUA 脚本开发之二】LUA语言基础
- lua基础【二】lua中关于字符串的操作总结
- 【Cocos2dx-Lua 脚本开发之二】Lua语言基础
- 【COCOS2DX-LUA 脚本开发之二】LUA语言基础
- 0基础lua学习(二十)Lua单例
- lua基础篇1
- lua 基础篇3
- lua基础篇
- Lua基础 类型和值(二)
- 用lua实现的不重复随机数
- c/c++函数和指针
- pflua, pflang, a luajit, in kernel engine for packet filter
- MySql安装心得
- c++基础(宏)
- lua基础篇(二)
- Android 三种方式实现自定义圆形页面加载中效果的进度条
- 生活常识
- C++基础(using)
- [译]Google C++编程风格指南(五)
- cocos2dx之控制台输出
- cocos2dx - SQlite
- SAX解析方式
- 查看局域网主机ip