Cocos2d之用Lua实现类

Cocos2d之用Lua实现类


Lua中的类

lua语言中实际上来说是没有类这个概念的,但是它也可以用自己的各种机制来实现类的效果。
从概念上来说,类有三大特性:封装性、继承性、多态性。lua有元表机制。
从结构上来说,类其实就是一个键值对的集合。lua有table可以满足。

lua的表查找键值对流程如下图所示:

lua表查找键值对流程

所以,我们通过lua的table及它的元表机制,可以在lua中实现类。


实现Lua类

在Cocos2d中,自己做了一个class方法,来作为类的机制。 文件位置: [cocos\scripting\lua-bindings\script\cocos2d\functions.lua]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
function class(classname, ...)
local cls = {__cname = classname}

local supers = {...}
for _, super in ipairs(supers) do
local superType = type(super)
assert(superType == "nil" or superType == "table" or superType == "function",
string.format("class() - create class \"%s\" with invalid super class type \"%s\"",
classname, superType))

if superType == "function" then
assert(cls.__create == nil,
string.format("class() - create class \"%s\" with more than one creating function",
classname));
-- if super is function, set it to __create
cls.__create = super
elseif superType == "table" then
if super[".isclass"] then
-- super is native class
assert(cls.__create == nil,
string.format("class() - create class \"%s\" with more than one creating function or native class",
classname));
cls.__create = function() return super:create() end
else
-- super is pure lua class
cls.__supers = cls.__supers or {}
cls.__supers[#cls.__supers + 1] = super
if not cls.super then
-- set first super pure lua class as class.super
cls.super = super
end
end
else
error(string.format("class() - create class \"%s\" with invalid super type",
classname), 0)
end
end

cls.__index = cls
if not cls.__supers or #cls.__supers == 1 then
setmetatable(cls, {__index = cls.super})
else
setmetatable(cls, {__index = function(_, key)
local supers = cls.__supers
for i = 1, #supers do
local super = supers[i]
if super[key] then return super[key] end
end
end})
end

if not cls.ctor then
-- add default constructor
cls.ctor = function() end
end
cls.new = function(...)
local instance
if cls.__create then
instance = cls.__create(...)
else
instance = {}
end
setmetatableindex(instance, cls)
instance.class = cls
instance:ctor(...)
return instance
end
cls.create = function(_, ...)
return cls.new(...)
end

return cls
end

几个设定键:

  • __cname : 类名
  • __create : 初始化方法
  • __supers : 父类集合

几个函数:

  • ctor : 构造函数
  • new : 初始化函数
  • create : 初始化函数(对new的一个封装,为了使用方法同C++部分一致)


class方法:

  1. 设置类名
  2. 遍历传入的构造方法
    • 若是 function类型,则设置为初始化方法
    • 若是 table类型
      • 若是 C++的类,设置初始化方法为调用原生类的create方法
      • 若是 纯lua类,将自己添加到基类中
  3. 处理自己的元表
    • ctor:必须要有
    • new
      • 获取instance值
      • 设置instance元表index
        • 若是 C++类,设置peer
          PS:peer是 tolua++提供的存储C++对象在Lua中的扩展
        • 若是 纯lua类,设置元表
      • 执行构造方法
      • 返回instance
    • create:对new的一个封装,为了使用方法同C++部分一致
  4. 处理构造函数、初始化函数的创建与封装





参考资料: