pairs()和ipairs()区别在于pairs()可以遍历整个table, 即包括数组及非数组部分
#!/usr/bin/lua
impl = {
"dddd","sss",
a=123,
["wee"]="one",
["two"]="linux"
}print("-------pairs------")
for key, val in pairs(impl) doprint(key, val)
end
print("-------ipairs------")
for key, val in ipairs(impl) doprint(key, val)
end
结果:
-------pairs------
1 dddd
2 sss
a 123
two linux
wee one
-------ipairs------
1 dddd
2 sss
函数调用 点号和 冒号 区别:
#!/usr/bin/lua
impl = {
"dddd","sss",
a=123,
["wee"]="one",
["two"]="linux"
}print("-function with impl---")
impl.get_print = function(impl, a, b)--print("a and b",a,b)return a,b
endaa, bb = impl:get_print(1,2)
print(aa, bb)print("-function without impl---")
impl.get_print = function(a, b)--print("a and b",a,b)return a,b
endaa, bb = impl.get_print(1,2)
print(aa, bb)
结果:
-function with impl---
1 2
-function without impl---
1 2