【发布时间】:2015-07-31 22:23:12
【问题描述】:
我正在尝试为每个lua-resty-redis、lua-resty-memcached 和lua-resty-mysql 模块编写一个扩展默认模块的小类。在我的子类中,我想从父类调用一个函数,但无论我阅读过什么 Lua 继承文档,都找不到合适的方法。
例如,我想覆盖connect() 函数,做一些事情并在某个时候调用父级的connect() 函数。但是怎么做呢?
local redis = require "resty.redis"
function redis.connect(self, ...)
-- Do some stuff here
local ok, err = parent:connect(...)
-- Do some other stuff here
return ok, err
end
如何做到这一点?
请注意,上述所有模块的结构如下:
local _M = { _VERSION = "0.1" }
local mt = { __index = _M }
function _M.new(self)
return setmetatable({ foo = "bar" }, mt)
end
function _M.connect(self, ...)
-- Connect
end
return _M
提前谢谢你!
【问题讨论】: