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:元方法之一,用於指定表的弱引用特性;

    1key值弱引用,只要其他地方沒有對key值引用,那麼,table自身的這個字段也會被刪除。設置方法:setmetatable(t, {__mode = "k"});

    2value值弱引用,只要其他地方沒有對value值引用,那麼,table的這個value所在的字段也會被刪除。設置方法:setmetatable(t, {__mode = "v"});

    3keyvalue弱引用,規則一樣,但是keyvalue都同時生效,任意一個起作用時都會導致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()

    

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章