且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

即使我已经用一个参数声明了函数,Lua函数也需要两个参数

更新时间:2022-12-24 19:01:03

http://www.lua.org/pil/16.html

使用:语法声明函数时,会有一个未指定的参数"self",即该函数正在处理的对象.您可以使用冒号语法调用该方法:

When you declare the function using the : syntax there is an unspecified parameter 'self' which is the object the function is working on. You can call the method using the colon syntax:

util:foo("Hello World")

如果使用点符号,则将函数引用为util表中的条目,并且您必须自己传递"self".

If you use the dot notation, you are referencing the function as an entry in the util table and you have to pass 'self' yourself.

在用冒号声明foo的情况下,这两个调用是等效的:

With foo declared with a colon, these two calls are equivalent:

util:foo("Hello World")
util.foo(util, "Hello World")

要使用点语法对此进行声明,请执行以下操作:

To declare this the same with the dot syntax you would do this:

function util.foo(self, p)
  print (p or "p is nil")
end

util.foo = function(self, p)
  print (p or "p is nil")
end