我正在尝试为每个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
先感谢您!