麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發 > 綜合 > 正文

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 調用成員函數。這個技巧并不非常有用,從效率角度上說,還是不用為好。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 羞羞色网站| 国产成人精品一区在线播放 | 日本aaaa片毛片免费观蜜桃 | 激情黄页 | 午夜国产在线观看 | 特级黄色一级毛片 | 欧美一级黄色片在线观看 | 看免费毛片 | 秋霞a级毛片在线看 | 国产精品69久久 | 最新精品在线 | a网站在线| 久色乳综合思思在线视频 | av在线免费观看网址 | 成人一区二区三区在线 | 久草热久 | 久草成人在线 | 91精品国产综合久久婷婷香蕉 | 成人午夜在线观看视频 | 欧美视频国产精品 | 欧美一级淫片007 | 成人一级视频在线观看 | 欧美视频在线一区二区三区 | 黄色作爱视频 | 国产一级在线免费观看 | 最新在线中文字幕 | 在线男人天堂 | 国产精品999在线 | 高清一区二区在线观看 | 视频一区二区三区在线播放 | 国产伊人色 | 欧美日韩国产成人在线观看 | av电影在线网站 | 中文字幕观看 | 手机黄色小视频 | 欧美日韩在线视频一区 | 国产乱乱视频 | 免费激情视频网站 | 免费看一级片 | 国产亚洲欧美视频 | 97精品视频在线观看 |