我使用这个变量创建了一个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我想将所有变量和函数从 to 复制BaseModule(ChildModule假设继承它们)并将一些额外的方法和变量添加到新模块。
问题是当我打电话时
ChildModule.get_template
我希望它会返回/CHILDMODULE_PATH/file.tmpl,但不会。它返回/BASEMODULE_PATH/file.tmpl。
但是,当我访问ChildModule.template_path它时,它包含正确的值(来自ChildModule)。
我该怎么做才能让 LuaChildModule在方法中使用变量ChildModule.get_template但不使用BaseModule(父模块)变量?Lua 中没有这个对象,所以我如何告诉 Lua 使用当前值?