1

我正在尝试为每个lua-resty-redislua-resty-memcachedlua-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

先感谢您!

4

1 回答 1

1
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
end
于 2015-07-31T14:23:12.903 回答