首页 > 编程知识 正文

冒号和引号的正确用法,英语中冒号的用法表

时间:2023-05-04 04:48:42 阅读:285663 作者:4083

lua 的 冒号和点的用法


无论在定义还是调用时,都可以使用: 和 .  。 定义中 如果是: 则省略了 self参数,但实际上参数中是有self的;如果是 . 则可以根据需要写上self。 调用时 使用:则会自动增加self为第一个参数;使用. 则要根据定义中的参数一一匹配,如果定义中是用的:则要增加self。


看一下下面的例子就明白了

a = {x = 3, y = 4}a.__index = function(table, key) print("start metatable") return a[key] endfunction a:new2(o) o = o or {} print("new2") setmetatable(o, self) return oendfunction a.new(self,o) o = o or {} print("new") setmetatable(o, self) return oendfunction a.new3(o) o = o or {} print("new3") setmetatable(o, a) return oendlocal b = a:new() --调用可以等价于a.new(a, {})local c = a.new(a)local d = a.new2(a)local e = a:new2()local f = a.new3()local g = a:new3() -- 无法执行 ,第一个是selfprint(getmetatable(b))print(getmetatable(c))print(getmetatable(d))print(getmetatable(e))print(getmetatable(f))print(getmetatable(g))print(b.x)print(b.y)print(c.x)print(c.y)print(d.x)print(d.y)print(e.x)print(e.y)print(f.x)print(f.y)print(g.x)print(g.y)

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。