在lua中,可以通过元表来实现类、对象、继承等。与元表相关的方法有setmetatable()、__index、getmetatable()、__newindex。
具体什么是元表在这里就不细说了,网上很多介绍,这里主要讲与cocos2dx相关联的部分。
在lua-binding库中extern.lua里,有如下方法:
--Create an class.
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
end
cls.ctor