【发布时间】:2013-01-21 09:52:26
【问题描述】:
我创建了一个名为 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 使用当前值?
【问题讨论】:
-
get_template函数无法编译。我想你在发帖时忘记了括号?
标签: inheritance lua