主頁 > 知識庫 > Lua元表與元方法實例講解

Lua元表與元方法實例講解

熱門標簽:百度競價排名 鐵路電話系統(tǒng) 網(wǎng)站排名優(yōu)化 服務(wù)外包 Linux服務(wù)器 地方門戶網(wǎng)站 呼叫中心市場需求 AI電銷

Lua中提供的元表(metatable)與元方法(metamethod)是一種非常重要的語法,metatable主要用于做一些類似于C++重載操作符式的功能。

Lua中提供的元表是用于幫助lua變量完成某些非預(yù)定義功能的個性化行為,如兩個table的相加,通過讓兩者指向同一元表并修改該元表的元方法可以實現(xiàn)該功能。

任何table都可以成為任何值的元表,而一組相關(guān)的table也可以共享一個元表。

一些MetaMethod:

復(fù)制代碼 代碼如下:

__add(a, b)                     對應(yīng)表達式 a + b
__sub(a, b)                     對應(yīng)表達式 a - b
__mul(a, b)                     對應(yīng)表達式 a * b
__div(a, b)                     對應(yīng)表達式 a / b
__mod(a, b)                     對應(yīng)表達式 a % b
__pow(a, b)                     對應(yīng)表達式 a ^ b
__unm(a)                        對應(yīng)表達式 -a
__concat(a, b)                  對應(yīng)表達式 a .. b
__len(a)                        對應(yīng)表達式 #a
__eq(a, b)                      對應(yīng)表達式 a == b
__lt(a, b)                      對應(yīng)表達式 a b
__le(a, b)                      對應(yīng)表達式 a = b
__index(a, b)                   對應(yīng)表達式 a.b
__newindex(a, b, c)             對應(yīng)表達式 a.b = c
__call(a, ...)                  對應(yīng)表達式 a(...)

1、算術(shù)類and關(guān)系類元方法

先看一個簡單的例子:

復(fù)制代碼 代碼如下:

--我們想讓兩個分數(shù)相加,這是一種非預(yù)定義的行為

fraction_a = {numerator=2, denominator=3}
fraction_b = {numerator=4, denominator=7}
fraction_op={}   --元表

-- __add這是metatable,這是lua內(nèi)建約定的
function fraction_op.__add(a,b)   
  res={}
  res.numerator=a.numerator*b.denominator+b.numerator*a.denominator
  res.denominator=a.denominator*b.denominator
  return res
end

--將fraction_a,fraction_b的元表設(shè)置為fraction_op
--其中setmetatable是庫函數(shù)
setmetatable(fraction_a,fraction_op)
setmetatable(fraction_b,fraction_op)

--調(diào)用的是fraction_op.__add()函數(shù)
fraction_c=fraction_a+fraction_b
print(fraction_c.numerator.."/"..fraction_c.denominator)
--輸出結(jié)果
--26/21

再來看一個深度一點的例子,例舉了算數(shù)類的元方法,關(guān)系類的元方法,庫定義的元方法。

復(fù)制代碼 代碼如下:

Set={}

local metatable={}  --元表

--根據(jù)參數(shù)列表中的值創(chuàng)建一個新的集合
function Set.new(a)
   local set={}
   --將所有由該方法創(chuàng)建的集合的元表都指定到metatable
   setmetatable(set,metatable)
   for i,v in pairs(a) do
       set[v]=true
   end
   return set
end

--計算兩個集合的并集
function Set.union(a,b)
   local res=Set.new{}
   for i in pairs(a) do
      res[i]=true
   end
   for i in pairs(b) do
      res[i]=true
   end
   return res
end

--計算兩個集合的交集
function Set.intersect(a,b)
  local res=Set.new{}
  for i in pairs(a) do
     res[i]=b[i]
  end
  return res
end

--print總是調(diào)用tostring來格式化輸出
--這里我們稍作修改庫定義的print
function Set.tostring(a)
  local t={}
  for i in pairs(a) do
     t[#t+1]=i
  end
  return "{"..table.concat(t,",").."}"
end

--判斷a集合是否是b集合的子集
function Set.lessorequal(a,b)
   for i in pairs(a) do
       if  not b[i] then return false end
   end
   return true
end

--最后將重定向的元方法加入到元表中
metatable.__add=Set.union
metatable.__mul=Set.intersect
metatable.__tostring=Set.tostring
metatable.__le=Set.lessorequal
metatable.__eq=function(a,b) return a=b and b=a end
metatable.__lt=function(a,b) return a=b and not (b=a) end

s1=Set.new{2,9,8,4}
s2=Set.new{2,4,7}
s3=s1+s2
s4=s1*s2
print(s3)
print(s4)
print(3+4,3*4)  --新加的方法不改變表本身具有的方法,因為傳入的參數(shù)不同,只會讓元方法更完善
s5=Set.new{2,4}
s6=Set.new{2,4,6}
print(s5=s6)
print(s5s6)
print(s5==s6)
--輸出結(jié)果
--{2,8,4,9,7}
--{2,4}
--7  12
--true
--true
--false

2、table訪問的元方法:

算數(shù)類和關(guān)系類的元方法都為各自錯誤情況定義了行為,他們不會改變語言的常規(guī)行為,但lua還是提供了一種可以改變table的行為。有兩種可以改變table的行為:查詢table以及修改table中不存在的字段。

1)、__index元方法

   當訪問table中不存在的字段時,得到的結(jié)果為nil。如果我們?yōu)閠able定義了元方法__index,那訪問的結(jié)果將由該方法決定。

復(fù)制代碼 代碼如下:

Window={}
Window.prototype={x=10,y=20,width=100,height=200}
Window.mt={} --Window的元表

function Window.new(o)
  setmetatable(o,Window.mt)
  return o
end

Window.mt.__index=function(table,key)  return Window.prototype[key] end

w=Window.new{x=1,y=22}
print(w.width)
print(w.width1)
--輸出結(jié)果
--100
--nil

2)、__newindex元方法

和__index不同的是,該元方法用于不存在鍵的賦值,而前者用于訪問。

復(fù)制代碼 代碼如下:

Window={}
Window.prototype={x=10,y=20,width=100,height=200}
Window.mt={} --Window的元表

function Window.new(o)
  setmetatable(o,Window.mt)
  return o
end

Window.mt.__index=function(table,key)  return Window.prototype[key] end
Window.mt.__newindex=function(table,key,value) Window.prototype[key]=value end

w=Window.new{x=1,y=22}
w.length=50
print(w.width)
print(w.width1)
print(Window.prototype.length)
--輸出結(jié)果
--100
--nil
--50

您可能感興趣的文章:
  • Lua中使用元表(metatable)執(zhí)行算術(shù)類元方法實例
  • Lua中的元表(metatable)、元方法(metamethod)詳解
  • Lua中的元表與元方法學(xué)習總結(jié)

標簽:黃山 蘭州 銅川 湖南 湘潭 仙桃 衡水 崇左

巨人網(wǎng)絡(luò)通訊聲明:本文標題《Lua元表與元方法實例講解》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266