国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

Lua中實現面向對象的一種漂亮解決方案

2024-07-21 23:04:22
字體:
來源:轉載
供稿:網友

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

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

這里提供 Lua 中實現 OO 的一種方案:

復制代碼 代碼如下:

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

 

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

復制代碼 代碼如下:

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

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

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

現在可以試一下了:
復制代碼 代碼如下:

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

 

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

復制代碼 代碼如下:

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

 

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

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

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


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

 

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

復制代碼 代碼如下:

function test:ctor()    -- 定義 test 的構造函數
    print("test ctor")
end

 

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


現在可以試一下了:
復制代碼 代碼如下:

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

其實,實現多重繼承也并不復雜,這里就不再展開了。更有意義的擴展可能是增加一個 dtor :)

 

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 和平区| 昭通市| 冷水江市| 巧家县| 宕昌县| 古蔺县| 固阳县| 宣城市| 丰镇市| 八宿县| 罗源县| 湖北省| 车险| 黄梅县| 新竹市| 曲靖市| 淳化县| 高台县| 鄢陵县| 德惠市| 临澧县| 高雄市| 孟州市| 四平市| 侯马市| 嘉善县| 广东省| 钦州市| 定安县| 伊通| 峨眉山市| 凭祥市| 绩溪县| 洱源县| 永州市| 施秉县| 改则县| 民乐县| 同德县| 清苑县| 张家港市|