我正在尝试为每个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提前谢谢你!
发布于 2015-07-31 22:23:13
local redis = require "resty.redis"
local original_connect = redis.connect
function redis.connect(self, ...)
-- Do some stuff here
local ok, err = original_connect(self, ...)
-- Do some other stuff here
return ok, err
endhttps://stackoverflow.com/questions/31694099
复制相似问题