Lua - 模块内部的变量命名空间

我创建了一个名为 BaseModule 的模块,其中包含变量 template_path 和函数 get_template,该函数使用这个变量:

module("BaseModule", package.seeall)
template_path = '/BASEMODULE_PATH/file.tmpl'
function get_template()
  print(template_path)
end

然后,我创建了另一个名为 "ChildModule" 的模块:

local BaseModule = require "BaseModule"
module("ChildModule", package.seeall)
setmetatable(ChildModule, {__index = BaseModule})
template_path = '/CHILDMODULE_PATH/file.tmpl'
some_child_specific_variable = 1

通过 setmetatable,我希望将 BaseModule 的所有变量和函数(让我们称之为从它们“继承”)复制到 ChildModule 中,并向新模块添加一些额外的方法和变量。

问题在于,当我调用

ChildModule.get_template

我期望它返回**/CHILDMODULE_PATH/file.tmpl,但实际上它返回的是/BASEMODULE_PATH/file.tmpl**。

但是,当我访问 ChildModule.template_path 时,它包含正确的值(来自 ChildModule)。

我该怎么做才能让 Lua 在 ChildModule.get_template 方法中使用 ChildModule 变量,而不是使用 BaseModule(父模块)变量?在 Lua 中没有 this 对象,因此我该如何告诉 Lua 使用当前值?

点赞
用户1190388
用户1190388

我认为你仍在使用已弃用的 Lua 版本。无论如何,你需要使用一些函数来设置 BaseModule 内部的 template_path 值,并将基础中的 template_path 设置为 local。所以,类似这样的:

BaseModule

module("BaseModule", package.seeall)
local template_path = "/BASEMODULE_PATH/file.tmpl"
function get_template()
  print(template_path)
end
function set_template( sLine )
  template_path = sLine
end

ChildModule

local BaseModule = require "BaseModule"
module("ChildModule", package.seeall)
setmetatable(ChildModule, {__index = BaseModule})
ChildModule.set_template( "/CHILDMODULE_PATH/file.tmpl" )
some_child_specific_variable = 1
ChildModule.get_template()

由于你进行了继承,所以你_不应该_直接设置基础模块的全局变量。

2013-01-21 10:40:40
用户1442917
用户1442917

我认为你试图操作变量而你可能想要操作你所创建对象的属性。也许像这样:

-- base.lua
local M = {}
M.template_path = '/BASEMODULE_PATH/file.tmpl'
function M:get_template()
  return self.template_path
end
return M

-- child.lua
local M = {}
setmetatable(M, {__index = require "base"})
M.template_path = '/CHILDMODULE_PATH/file.tmpl'
M.some_child_specific_variable = 1
return M

-- main.lua
local base = require "base"
local child = require "child"

print(base:get_template(), child:get_template(),
  child.some_child_specific_variable)

这将打印出你预期的结果:

/BASEMODULE_PATH/file.tmpl  /CHILDMODULE_PATH/file.tmpl 1

顺便说一下,你可以把 child.lua 变成一行:

return setmetatable({template_path = '/CHILDMODULE_PATH/file.tmpl',
  some_child_specific_variable = 1}, {__index = require "base"})

虽然不建议这样做,但是你可以这样做。

2013-01-22 06:13:23