Lua:函数表
我试图在表中存储不同的函数,但不知何故它不会按照我想象的方式工作。这是我的“代码”
fn_table = { aFun1=print, aFun2=self:getSpeedLevel, aFun3=.... }
现在的问题是,我可以使用 print
、assert
等内置函数来执行此操作,但它无法与我的其他函数一起使用已经有了。
我收到错误:“...函数参数应位于 '}' 附近
是否也可以存储这些函数?
I am trying to store different functions in a table, but somehow it won't work the way I thought it would. Here is my 'code'
fn_table = { aFun1=print, aFun2=self:getSpeedLevel, aFun3=.... }
The problem now is that I can do this with the built in functions like print
, assert
and so on but it wont work with the other functions I've got.
I get the error: "... function arguments expected near '}'
Is it possible to store these function as well?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
aFun2 = self:getSpeedLevel
是一个语法错误,这就是 Lua 抱怨的地方。尝试aFun2 = getSpeedLevel
或aFun2 = self.getSpeedLevel
(假设self
是一个表)。 PiL 书中的面向对象编程章节提供了更多将函数存储在表中的示例。aFun2 = self:getSpeedLevel
is a syntax error and that is what Lua complains about. TryaFun2 = getSpeedLevel
oraFun2 = self.getSpeedLevel
(assuming thatself
is a table). The Object-Oriented Programming chapter in the PiL book has more examples of functions being stored in tables.