LUA面向对象编程技巧

详文请见 http://ffown.sinaapp.com/?p=11

1. LUA中的对象

我们知道,对象由属性和方法组成。LUA中最基本的结构是table,So 必须用table描述对象的属性。lua中的function可以用来表示方法。那么LUA中的类

可以通过table + function模拟出来。至于继承,可以通过metetable模拟出来(不推荐用,只模拟最基本的对象大部分时间够用了)。

2. Metatable

Lua中的metatable 类似于C++中的虚函数,当索引table中的项不存在时,会进一步索引metetable(如果设置了的话)是否存在该项。这跟虚函数概念

不是很吻合么?

3. 示例class

user_t = {}

user_t.__index = user_t

以上代码声明class user_t。为了方便,user_t声明为全局的table。__index 是跟设置metatable有关,详细信息参见lua manual http://www.lua.org/manual/5.1/

实际上__index 应该赋值为function,这里是一个语法糖,等价于

user_t.__index = function(key) return user_t[key] end 

定义构造函数:

function user_t:new(uid_)
local obj =
{
["uid"] = uid_,
}
setmetatable(obj, self)
return obj
end
function user_t:dump()
 print("self:", self.uid)
end

定义一个user_t对象代码如下:

local user = user_t:new(1122334)

user:dump()

new 函数返回一个table, 当索引dump时,obj中没有dump函数,尝试去metatable中索引,获得dump函数。

注意:

function user_t.dump(self) :方式定义函数只是个语法糖而已,其等价于
function user_t.dump(self)
print("self:", self.uid)
end

通常我都会对应定义一个析构函数(不好意思C++流)

function user_t:delete()
self.uid = nil
end

4. 实现继承

原理就是利用metatable,__index 设置这样的function,当前类不存在某项时,尝试去基类中查出

person_t = {}
person_t.__index = person_t

function person_t:new()
local obj =
{
["type"] = "person",
}
setmetable(person_t, self)
return obj
end

function person_t:type()
print("type:", self["type"])
end

function user_t:new(uid_)
local obj =
{
["uid"] = uid_,
}
local mt =
{
["__index"] = function(key_)
local ret = user_t[key_] or person_t[key_]
return ret
end
}
setmetatable(obj, mt)
return obj
end
local user = user_t:new(1122334)
user:type()

5. 注意

1. 尽量不要使用多重继承

2. 不要尝试使用重载

 更多精彩文章 http://h2cloud.org

 

原文链接: https://www.cnblogs.com/zhiranok/archive/2012/02/07/lua_object_skill.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    LUA面向对象编程技巧

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/41069

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月8日 下午5:59
下一篇 2023年2月8日 下午5:59

相关推荐