Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lua没有类,只有点运算符?_Lua - Fatal编程技术网

Lua没有类,只有点运算符?

Lua没有类,只有点运算符?,lua,Lua,如果Lua没有类,为什么它有点运算符 例如,在string.find中,string是一个具有静态/class方法的类find?在您的示例中,find是表string 它是string[“find”] 可以这样定义: local string = { "find" = function() -- find stuff end } 或 或 在您的示例中,find是表string 它是string[“find”] 可以这样定义: local string = { "find"

如果Lua没有类,为什么它有点运算符


例如,在
string.find
中,
string
是一个具有静态/class方法的类
find

在您的示例中,
find
是表
string

它是
string[“find”]

可以这样定义:

local string = {
  "find" = function()
    -- find stuff
  end
}


在您的示例中,
find
是表
string

它是
string[“find”]

可以这样定义:

local string = {
  "find" = function()
    -- find stuff
  end
}

你好 使用Lua5.3-字符串是元表的。因此,如果您知道与哪些函数相关,您可以直接使用它或尝试在metatable中定义的函数。 “在元表中”意味着用点来获取它

str='Doomsday starts with: Tah tah tah Taaaahhh'
print(#str)
print(getmetatable(str).__index.len(str))
两者都是输出字符串长度的相同函数:42

字符串的元表包含

…函数,让我们展示它们

show(getmetatable(str).__index)
len=function=function: 0x565fe2c0
pack=function=function: 0x566005f0
dump=function=function: 0x565ffc80
gsub=function=function: 0x56601560
format=function=function: 0x565ff030
byte=function=function: 0x565fe4b0
packsize=function=function: 0x56600110
char=function=function: 0x565fef10
reverse=function=function: 0x565fe830
rep=function=function: 0x565fe9f0
find=function=function: 0x56601550
upper=function=function: 0x565fe7a0
sub=function=function: 0x565fe8c0
gmatch=function=function: 0x565fee70
unpack=function=function: 0x56600250
match=function=function: 0x56601540
lower=function=function: 0x565feba0
…count()和show()是用于表的简单自脚本函数

function count(array) local incr=0 for _ in pairs(array) do incr=incr+1 end return incr end                                               
                                                                                                                                               
function show(array) for key,value in pairs(array) do printf("%s=%s=%s\n",key,type(value),value) end end                             
                                                                                                                                               
function printf(s,...) io.write(s:format(...)) end
字符串有一个元表这一事实让我尝试设置_调用并测试它

…呵呵,这是工作,输出是

Doomsday starts with: Tah tah tah Taaaahhh  I do not like apples!
你好 使用Lua5.3-字符串是元表的。因此,如果您知道与哪些函数相关,您可以直接使用它或尝试在metatable中定义的函数。 “在元表中”意味着用点来获取它

str='Doomsday starts with: Tah tah tah Taaaahhh'
print(#str)
print(getmetatable(str).__index.len(str))
两者都是输出字符串长度的相同函数:42

字符串的元表包含

…函数,让我们展示它们

show(getmetatable(str).__index)
len=function=function: 0x565fe2c0
pack=function=function: 0x566005f0
dump=function=function: 0x565ffc80
gsub=function=function: 0x56601560
format=function=function: 0x565ff030
byte=function=function: 0x565fe4b0
packsize=function=function: 0x56600110
char=function=function: 0x565fef10
reverse=function=function: 0x565fe830
rep=function=function: 0x565fe9f0
find=function=function: 0x56601550
upper=function=function: 0x565fe7a0
sub=function=function: 0x565fe8c0
gmatch=function=function: 0x565fee70
unpack=function=function: 0x56600250
match=function=function: 0x56601540
lower=function=function: 0x565feba0
…count()和show()是用于表的简单自脚本函数

function count(array) local incr=0 for _ in pairs(array) do incr=incr+1 end return incr end                                               
                                                                                                                                               
function show(array) for key,value in pairs(array) do printf("%s=%s=%s\n",key,type(value),value) end end                             
                                                                                                                                               
function printf(s,...) io.write(s:format(...)) end
字符串有一个元表这一事实让我尝试设置_调用并测试它

…呵呵,这是工作,输出是

Doomsday starts with: Tah tah tah Taaaahhh  I do not like apples!

点运算符通常用于从结构/记录获取命名字段/成员。点运算符通常用于从结构/记录获取命名字段/成员。