濮阳杆衣贸易有限公司

主頁 > 知識庫 > Lua中實(shí)現(xiàn)面向?qū)ο蟮囊环N漂亮解決方案

Lua中實(shí)現(xiàn)面向?qū)ο蟮囊环N漂亮解決方案

熱門標(biāo)簽:清朝地圖標(biāo)注哈爾濱 冀州市地圖標(biāo)注 怎么去除地圖標(biāo)注 百度地圖標(biāo)注早餐區(qū)域 武漢外呼防封系統(tǒng)多少錢 漳州智云呼電話機(jī)器人 個人怎么在地圖標(biāo)注需要的店鋪 新岸線智能電銷機(jī)器人 地圖標(biāo)注大廈

在 pil 中,lua 的作者推薦了一種方案來實(shí)現(xiàn) OO,比較簡潔,但是我依然覺得有些繁瑣。

這里給出一種更漂亮一點(diǎn)的解決方案,見下文:

這里提供 Lua 中實(shí)現(xiàn) OO 的一種方案:

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

local _class={}
 
function class(super)
 local class_type={}
 class_type.ctor=false
 class_type.super=super
 class_type.new=function(...)
   local obj={}
   do
    local create
    create = function(c,...)
     if c.super then
      create(c.super,...)
     end
     if c.ctor then
      c.ctor(obj,...)
     end
    end
 
    create(class_type,...)
   end
   setmetatable(obj,{ __index=_class[class_type] })
   return obj
  end
 local vtbl={}
 _class[class_type]=vtbl
 
 setmetatable(class_type,{__newindex=
  function(t,k,v)
   vtbl[k]=v
  end
 })
 
 if super then
  setmetatable(vtbl,{__index=
   function(t,k)
    local ret=_class[super][k]
    vtbl[k]=ret
    return ret
   end
  })
 end
 
 return class_type
end

現(xiàn)在,我們來看看怎么使用:
base_type=class()  -- 定義一個基類 base_type

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

function base_type:ctor(x) -- 定義 base_type 的構(gòu)造函數(shù)
 print("base_type ctor")
 self.x=x
end
 
function base_type:print_x() -- 定義一個成員函數(shù) base_type:print_x
 print(self.x)
end
 
function base_type:hello() -- 定義另一個成員函數(shù) base_type:hello
 print("hello base_type")
end

以上是基本的 class 定義的語法,完全兼容 lua 的編程習(xí)慣。我增加了一個叫做 ctor 的詞,作為構(gòu)造函數(shù)的名字。
下面看看怎樣繼承:
復(fù)制代碼 代碼如下:

test=class(base_type) -- 定義一個類 test 繼承于 base_type
 
function test:ctor() -- 定義 test 的構(gòu)造函數(shù)
 print("test ctor")
end
 
function test:hello() -- 重載 base_type:hello 為 test:hello
 print("hello test")
end

現(xiàn)在可以試一下了:
復(fù)制代碼 代碼如下:

a=test.new(1) -- 輸出兩行,base_type ctor 和 test ctor 。這個對象被正確的構(gòu)造了。
a:print_x() -- 輸出 1 ,這個是基類 base_type 中的成員函數(shù)。
a:hello() -- 輸出 hello test ,這個函數(shù)被重載了。

在這個方案中,只定義了一個函數(shù) class(super) ,用這個函數(shù),我們就可以方便的在 lua 中定義類:

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

base_type=class()       -- 定義一個基類 base_type

function base_type:ctor(x)  -- 定義 base_type 的構(gòu)造函數(shù)
    print("base_type ctor")
    self.x=x
end

function base_type:print_x()    -- 定義一個成員函數(shù) base_type:print_x
    print(self.x)
end

function base_type:hello()  -- 定義另一個成員函數(shù) base_type:hello
    print("hello base_type")
end


以上是基本的 class 定義的語法,完全兼容 lua 的編程習(xí)慣。我增加了一個叫做 ctor 的詞,作為構(gòu)造函數(shù)的名字。

下面看看怎樣繼承: test=class(basetype) -- 定義一個類 test 繼承于 basetype

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

function test:ctor()    -- 定義 test 的構(gòu)造函數(shù)
    print("test ctor")
end

function test:hello()   -- 重載 base_type:hello 為 test:hello
    print("hello test")
end


現(xiàn)在可以試一下了:
復(fù)制代碼 代碼如下:

a=test.new(1)   -- 輸出兩行,base_type ctor 和 test ctor 。這個對象被正確的構(gòu)造了。
a:print_x() -- 輸出 1 ,這個是基類 base_type 中的成員函數(shù)。
a:hello()   -- 輸出 hello test ,這個函數(shù)被重載了。

其實(shí),實(shí)現(xiàn)多重繼承也并不復(fù)雜,這里就不再展開了。更有意義的擴(kuò)展可能是增加一個 dtor :)

ps. 這里用了點(diǎn)小技巧,將 self 綁定到 closure 上,所以并不使用 a:hello 而是直接用 a.hello 調(diào)用成員函數(shù)。這個技巧并不非常有用,從效率角度上說,還是不用為好。

您可能感興趣的文章:
  • Lua面向?qū)ο笾惡屠^承
  • Lua面向?qū)ο笾嘀乩^承、私密性詳解
  • Lua面向?qū)ο笾惡屠^承淺析
  • Lua中的面向?qū)ο缶幊淘斀?/li>
  • Lua 極簡入門指南(七):面向?qū)ο缶幊?/li>
  • Lua面向?qū)ο缶幊虒W(xué)習(xí)筆記
  • Lua模擬面向?qū)ο笫纠窒?/li>

標(biāo)簽:儋州 臺灣 德宏 天門 宣城 金昌 天門 濰坊

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Lua中實(shí)現(xiàn)面向?qū)ο蟮囊环N漂亮解決方案》,本文關(guān)鍵詞  Lua,中,實(shí)現(xià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)文章
  • 下面列出與本文章《Lua中實(shí)現(xiàn)面向?qū)ο蟮囊环N漂亮解決方案》相關(guān)的同類信息!
  • 本頁收集關(guān)于Lua中實(shí)現(xiàn)面向?qū)ο蟮囊环N漂亮解決方案的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    兴城市| 陆川县| 昭通市| 乾安县| 神农架林区| 朔州市| 兴隆县| 岑巩县| 靖安县| 藁城市| 神农架林区| 监利县| 格尔木市| 滕州市| 平南县| 勃利县| 黄平县| 通许县| 荥经县| 南岸区| 碌曲县| 凤阳县| 新郑市| 英德市| 武鸣县| 盐城市| 福鼎市| 古交市| 中卫市| 贺州市| 辽阳县| 维西| 竹溪县| 和林格尔县| 高尔夫| 衡东县| 吐鲁番市| 卫辉市| 通化县| 方正县| 仙游县|