項目中一直是使用functions 這個方法模擬面向對象,在cocos3.0 中提供的 functions.lua文件有定義 。
1、核心代碼爲:
注意實例化繼承自C++層類 和 lua類的區別
function class(classname, super)
local superType = type(super)
local cls
if superType ~= "function" and superType ~= "table" then
superType = nil
super = nil
end
if superType == "function" or (super and super.__ctype == 1) then
-- inherited from native C++ Object
cls = {}
if superType == "table" then
-- copy fields from super
for k,v in pairs(super) do cls[k] = v end
cls.__create = super.__create
cls.super = super
else
cls.__create = super
cls.ctor = function() end
end
cls.__cname = classname
cls.__ctype = 1
cls.__index = cls
function cls.new(...)
local instance = cls.__create(...);
-- copy fields from class to native object
for k,v in pairs(cls) do instance[k] = v end
instance.class = cls
instance:ctor(...);
return instance
end
else
-- inherited from Lua Object
if super then
cls = clone(super)
cls.super = super
else
cls = {ctor = function() end}
end
cls.__cname = classname
cls.__ctype = 2 -- lua
cls.__index = cls
function cls.new(...)
local instance = setmetatable({}, cls)
instance.class = cls
instance:ctor(...)
return instance
end
end
return cls
end
function clone(object)
local lookup_table = {}
local function _copy(object)
if type(object) ~= "table" then
return object
elseif lookup_table[object] then
return lookup_table[object]
end
local new_table = {}
lookup_table[object] = new_table
for key, value in pairs(object) do
new_table[_copy(key)] = _copy(value)
end
return setmetatable(new_table, getmetatable(object))
end
return _copy(object)
end
2、解釋圖注意實例化繼承自C++層類 和 lua類的區別
3、使用lua面向對象的注意事項
(1)lua層類的實例化是使用 instance = setmetatable( {}, cls)來模擬的,所以需要注意的是如果類A中有屬性 是一個table
屬性,如果類A的實例a改變了其中的一個屬性,那麼其他所有的A的類的對象都會訪問該屬性都是被修改了的。
輸出結果:
注意輸出A類的實例對象 b時候 b.tab.id 已被改變 【這點理解了 lua 元表的 metatable 的 __index 不難理解,但是使用Lua的面向對象時候需要注意】
2、繼承中調用構造函數需要助於的點,訪問父類的夠着函數時候用 類名訪問,而不要使用 self
如果將 X.super 改成 self.super 那麼在實例化C調用 self.init() 會出死是循環.
注意:
如果將 X.super 改成 self.super 那麼在實例化C調用 self.init() 會出死是循環.
注意:
self 指向傳入的對象. 使用 local c = C.new() c.init() 那麼其實在 B A 中使用的 self 都是C 的對象 c 出現 c.init() --> b.init() ---> b.init() ---> 死循環
而在面向對象的中的 this 是指向當前對象,這個需要注意lua面向對象self 與this 區別。
而在面向對象的中的 this 是指向當前對象,這個需要注意lua面向對象self 與this 區別。