lua基础篇(二)

元表

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()

    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值