自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Lua中關(guān)于元表和元方法學(xué)習(xí)教程

移動(dòng)開發(fā) iOS
Lua中關(guān)于元表和元方法學(xué)習(xí)是本文要介紹的內(nèi)容,主要來了解Lua中每個(gè)值都可具有元表。 元表是普通的Lua表,定義了原始值在某些特定操作下的行為。你可通過在值的原表中設(shè)置特定的字段來改變作用于該值的操作的某些行為特征。

Lua中關(guān)于元表元方法學(xué)習(xí)是本文要介紹的內(nèi)容,主要來了解Lua中每個(gè)值都可具有元表元表是普通的Lua表,定義了原始值在某些特定操作下的行為。你可通過在值的原表中設(shè)置特定的字段來改變作用于該值的操作的某些行為特征。例如,當(dāng)數(shù)字值作為加法的操作數(shù)時(shí),Lua檢查其元表中的"__add"字段是否有個(gè)函數(shù)。如果有,Lua調(diào)用它執(zhí)行加法。

我們稱元表中的鍵為事件(event),稱值為元方法(metamethod)。前述例子中的事件是"add",元方法是執(zhí)行加法的函數(shù)。

可通過函數(shù)getmetatable查詢?nèi)魏沃档脑怼?/p>

可通過函數(shù)setmetatable替換表的元表。不能從Lua中改變其他類型的元表(除了使用調(diào)試庫);必須使用C API才能做到。

表和完整的用戶數(shù)據(jù)具有獨(dú)立的元表(盡管多個(gè)表和用戶數(shù)據(jù)可共享元表);每種其他類型的所有值共享一個(gè)元表。所以,所有數(shù)字共享一個(gè)元表,字符串也是,等等。

元表可以控制對(duì)象的數(shù)學(xué)運(yùn)算、順序比較、連接、取長、和索引操作的行為。元表也能定義用戶數(shù)據(jù)被垃圾收集時(shí)調(diào)用的函數(shù)。Lua給這些操作的每一個(gè)都關(guān)聯(lián)了稱為事件的特定鍵。當(dāng)Lua對(duì)某值執(zhí)行其中一個(gè)操作時(shí),檢查該值是否含有元表以及相應(yīng)的事件。如果有,與該鍵關(guān)聯(lián)的值(元方法)控制Lua如何完成操作。

元表控制后面列舉的操作。每個(gè)操作由相應(yīng)的名字標(biāo)識(shí)。每個(gè)操作的鍵是由其名字前綴兩個(gè)下劃線“__”的字符串;例如,操作“加(add)”的鍵是字符串"__add"。這些操作的語義通過一個(gè)Lua函數(shù)描述解釋器如何執(zhí)行操作作了更好的說明。

下面顯示的Lua代碼只是說明性的;真實(shí)的行為被硬編碼到解釋器中,并且比這里的模擬更加高效。這些描述中的所有函數(shù)(rawget、tonumber等等。)在§5.1中描述。特別一提,要獲取給定對(duì)象的元方法,我們使用表達(dá)式

  1. metatable(obj)[event] 

它應(yīng)被解讀為

  1. rawget(getmetatable(obj) or {}, event) 

就是說,訪問一個(gè)元方法不會(huì)調(diào)用其他元方法,而且訪問沒有元表的對(duì)象不會(huì)失?。ㄖ皇墙Y(jié)果為nil)。

"add": + 操作。

下面的getbinhandler函數(shù)定義Lua如何選擇二元操作的處理程序。首先嘗試***操作數(shù),如果它的類型沒有定義該操作的處理程序,則嘗試第二操作數(shù)。

  1. function getbinhandler (op1, op2, event)  
  2.  return metatable(op1)[event] or metatable(op2)[event]       
  3. end 

通過應(yīng)用該函數(shù),op1 + op2的行為是

 

  1. function add_event (op1, op2)  
  2.  local o1, o2 = tonumber(op1), tonumber(op2)  
  3.  if o1 and o2 then  -- 兩操作數(shù)都是數(shù)字  
  4.   return o1 + o2   -- ‘+’此處是‘add’的原語  
  5.  else  -- 至少一個(gè)操作數(shù)不是數(shù)字    
  6.   local h = getbinhandler(op1, op2, "__add")  
  7.   if h then    -- 用兩個(gè)操作數(shù)調(diào)用處理程序  
  8.    return (h(op1, op2))    
  9.   else  -- 沒有可用的處理程序:缺省行為  
  10.    error(...)  
  11.   end         
  12.  end  
  13. end 
  1. "sub": - 操作。 行為類似于“add”操作。   
  2. "mul": * 操作。 行為類似于“add”操作。   
  3. "div": / 操作。 行為類似于“add”操作。   
  4. "mod": % 操作。 行為類似于“add”操作。以o1 - floor(o1/o2)*o2為操作原語。   
  5. "pow": ^ (取冪)操作。 行為類似于“add”操作,以函數(shù)pow(來自C數(shù)學(xué)庫)為操作原語。   
  6. "unm": 一元-操作。   
  7. function unm_event (op)  
  8.  local o = tonumber(op)  
  9.  if o then  -- 操作數(shù)是數(shù)字?  
  10.   return -o  -- ‘-’此處是‘unm’的原語  
  11.  else  -- 操作數(shù)不是數(shù)字  
  12.  -- 嘗試由操作數(shù)取得處理程序。  
  13.   local h = metatable(op).__unm  
  14.   if h then-- 用操作數(shù)調(diào)用處理程序  
  15.    return (h(op))  
  16.   else  -- 沒有可用的處理程序:缺省行為      
  17.    error(...)    
  18.   end  
  19.  end       
  20. end 

"concat": .. (連接)操作。

  1. function concat_event (op1, op2)  
  2.  if (type(op1) == "string" or type(op1) == "number") and   
  3.  (type(op2) == "string" or type(op2) == "number") then    
  4.   return op1 .. op2  -- 字符串連接原語         
  5.  else    
  6.   local h = getbinhandler(op1, op2, "__concat")    
  7.   if h then      
  8.    return (h(op1, op2))    
  9.   else      
  10.    error(...)    
  11.   end  
  12.  end       
  13. end    

"len": # 操作。

  1. function len_event (op)  
  2.  if type(op) == "string" then    
  3.   return strlen(op)  -- 取字符串長度原語         
  4.  elseif type(op) == "table" then    
  5.   return #op  -- 取表長度原語         
  6.  else    
  7.   local h = metatable(op).__len    
  8.   if h then    -- 用操作數(shù)調(diào)用處理程序      
  9.    return (h(op))    
  10.   else  -- 沒有可用的處理程序:缺省行為      
  11.    error(...)    
  12.   end         
  13.  end       
  14. end   

"eq": == 操作。 函數(shù)getcomphandler定義Lua如何選擇比較操作符的元方法。只有待比較的兩個(gè)對(duì)象類型和選定操作對(duì)應(yīng)的元方法都相同,才會(huì)選擇該元方法。

  1. function getcomphandler (op1, op2, event)         
  2.  if type(op1) ~= type(op2) then return nil end         
  3.  local mm1 = metatable(op1)[event]         
  4.  local mm2 = metatable(op2)[event]         
  5.  if mm1 == mm2 then   
  6.   return mm1   
  7.  else   
  8.   return nil   
  9.  end       
  10. end  
  11. "eq"事件定義如下:   
  12. function eq_event (op1, op2)         
  13.  if type(op1) ~= type(op2) then  -- 類型不同?    
  14.   return false   -- 對(duì)象不同         
  15.  end         
  16.  if op1 == op2 then   -- 相等原語?    
  17.   return true   -- 對(duì)象相同         
  18.  end       -- 嘗試元方法         
  19.  local h = getcomphandler(op1, op2, "__eq")         
  20.  if h then    
  21.   return (h(op1, op2))         
  22.  else    
  23.   return false         
  24.  end       
  25. end  
  26. a ~= b等價(jià)于not (a == b)。   
  27.  
  28. "lt": < 操作。   
  29. function lt_event (op1, op2)         
  30.  if type(op1) == "number" and type(op2) == "number" then    
  31.   return op1 < op2   -- 數(shù)字比較         
  32.  elseif type(op1) == "string" and type(op2) == "string" then    
  33.   return op1 < op2   -- 詞典順序比較         
  34.  else    
  35.   local h = getcomphandler(op1, op2, "__lt")    
  36.   if h then      
  37.    return (h(op1, op2))    
  38.   else      
  39.    error(...);    
  40.   end         
  41.  end      
  42. end  
  43. > b等價(jià)于b < a。   
  44.  
  45. "le": <= 操作。   
  46. function le_event (op1, op2)         
  47.  if type(op1) == "number" and type(op2) == "number" then    
  48.   return op1 <= op2   -- 數(shù)字比較         
  49.  elseif type(op1) == "string" and type(op2) == "string" then    
  50.   return op1 <= op2   -- 詞典順序比較         
  51.  else    
  52.   local h = getcomphandler(op1, op2, "__le")    
  53.   if h then      
  54.    return (h(op1, op2))    
  55.   else      
  56.    h = getcomphandler(op1, op2, "__lt")      
  57.    if h then        
  58.     return not h(op2, op1)      
  59.    else        
  60.     error(...);      
  61.    end    
  62.   end         
  63.  end       
  64. end 

a >= b等價(jià)于 b <= a。注意,假定a <= b等價(jià)于not (b < a),那么當(dāng)沒有“le”元方法時(shí),Lua嘗試“lt”。

  1. "index": 索引訪問table[key]。   
  2. function gettable_event (table, key)         
  3.  local h         
  4.  if type(table) == "table" then    
  5.   local v = rawget(table, key)    
  6.   if v ~= nil then   
  7.    return v   
  8.   end    
  9.   h = metatable(table).__index    
  10.   if h == nil then   
  11.    return nil   
  12.   end         
  13.  else    
  14.   h = metatable(table).__index    
  15.   if h == nil then      
  16.    error(...);    
  17.   end         
  18.  end         
  19.  if type(h) == "function" then    
  20.   return (h(table, key))     -- 調(diào)用處理程序         
  21.  else   
  22.   return h[key]    -- 對(duì)它重復(fù)上述操作         
  23.  end       
  24. end  
  25.  
  26. "newindex": 索引賦值table[key] = value。   
  27. function settable_event (table, key, value)         
  28.  local h         
  29.  if type(table) == "table" then    
  30.   local v = rawget(table, key)    
  31.   if v ~= nil then   
  32.    rawset(table, key, value);   
  33.    return   
  34.   end    
  35.   h = metatable(table).__newindex    
  36.   if h == nil then   
  37.    rawset(table, key, value);   
  38.   return   
  39.   end         
  40.  else   
  41.   h = metatable(table).__newindex    
  42.   if h == nil then      
  43.    error(...);    
  44.   end         
  45.  end         
  46.  if type(h) == "function" then    
  47.   h(table, key,value)    -- 調(diào)用處理程序         
  48.  else   
  49.   h[key] = value      -- 對(duì)它重復(fù)上述操作         
  50.  end       
  51. end     
  52.  
  53. "call": 當(dāng)Lua調(diào)用值時(shí)被調(diào)用。   
  54. function function_event (func, ...)         
  55.  if type(func) == "function" then    
  56.   return func(...)   -- 調(diào)用原語         
  57.  else    
  58.   local h = metatable(func).__call    
  59.   if h then      
  60.    return h(func, ...)    
  61.   else      
  62.    error(...)    
  63.   end         
  64.  end       
  65. end   

小結(jié):Lua中關(guān)于元表元方法學(xué)習(xí)教程的內(nèi)容介紹完了,希望通過本文的學(xué)習(xí)能對(duì)你有所幫助!

責(zé)任編輯:zhaolei 來源: 博客園
相關(guān)推薦

2011-08-31 15:41:38

Lua解釋器

2011-08-23 16:37:05

Lua數(shù)學(xué)庫

2011-08-24 17:09:35

LUA閉包函數(shù)

2011-08-23 15:34:56

Lua模式 匹配

2011-08-24 14:14:13

LUA環(huán)境 配置

2011-08-24 13:27:07

Lua 游戲C接口腳本

2011-08-23 17:06:03

2011-08-23 16:48:41

Lua 5.1API 函數(shù)

2011-08-23 13:54:10

LUA全局變量

2011-08-24 15:42:38

LUA源代碼

2011-08-25 15:41:42

Lua源碼

2011-08-24 15:34:44

MinGWLua環(huán)境配置

2011-08-24 11:08:09

Lua

2011-08-25 17:01:50

LUA網(wǎng)游游戲

2011-08-25 16:20:33

Lua腳本變量

2011-08-23 17:33:08

LuaMetatable

2011-07-15 13:49:30

C++友元函數(shù)友元類

2011-08-23 16:22:45

Lua 4.0函數(shù)

2021-08-23 10:14:20

鴻蒙HarmonyOS應(yīng)用

2022-07-04 23:24:17

元宇宙虛擬世界倫理問題
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)