Lua本身是没有面向对象支持的,但面向对象编程在逻辑复杂的大型工程却很有用。于是很多人用Lua本身的数据结构table来模拟面向对象。最简单的一种方法是把对象的方法、成员都放到table中。如:
-- file:test.lua local test = {} function test:get_x() return self.x or 0 end function test:set_x( _x ) self.x = _x end local test_module = {} function test_module.new() local t = {} for k,v in pairs( test ) do t[k] = v end return t end return test_module |
调用也比较简单:
-- file:main.lua
local test = require "test"
local _t = test.new()
_t:set_x( 999 )
print( _t:get_x() )
这已经很像面向对象编程。但我们可以看到这样写有些缺点:
1.数据和方法混在一起(当然这不是什么大问题,C++也是这样)
2.每创建一个对象,都要将方法复制一遍
3.没法继承
Lua有强大的元表(metatable),利用它我们可以更优雅地封装一下:
1.先统一封装一个面向对象函数:
-- file:oo.lua local oo = {} local cls = {} local function new( clz ) local t = {} setmetatable(t, clz) return t end function oo.class( parent,name ) local t = {} cls[name] = t parent = parent or {} rawset( t,"__index",t ) setmetatable( t,{ __index = parent,__call = new } ) return t end return oo |
2.然后重新写类的实现:
-- file:test.lua local oo = require "oo" local test = oo.class( nil,... ) function test:get_x() return self.x or 0 end function test:set_x( _x ) self.x = _x end return test |