Lua点与冒号的区别

local _Tab = {[1] = "Hello Lua",x = 10}

--通过点调用一个普通的方法
function _Tab.BasicFunc()
    print("I'm a BasicFunc")
end

--通过点来调用并且传递一个自身
function _Tab.FuncWithSelf(selfTable)
    print("FuncWithSelf".." _Tab ")
    print(_Tab)

    print("FuncWithSelf".." selfTable ")
    print(selfTable)    
end

--通过点来调用,传递一个自身并且再传递一个值
function _Tab.FuncWithSelfArg(selfTable,otherArg)
    print("_Tab")
    print(_Tab)

    print("FuncWithSelfArg".." selfTable ")
    print(selfTable)

    print("FuncWithSelfArg".." otherArg ")
    print(otherArg)
end

--通过冒号来实现一个无参数方法
function _Tab:ColonFuncNoParam()
    print("ColonFuncNoParam".." _Tab ")
    print(_Tab)

    print("ColonFuncNoParam".." self ")
    print(self)
end

--通过冒号来实现一个有参数的方法
function _Tab:ColonFuncWithParam(arg)
    print("ColonFuncWithParam".." self ")
    print(self)
    
    print("ColonFuncWithParam".." arg ")
    print(arg)
end

Lua方法调用. :

方法的使用

  • 冒号操作会带入一个 self 参数,用来代表 自己。而点号操作,只是 内容 的展开。
  • 在函数定义时,使用冒号将默认接收一个 self参数,而使用点号则需要显式传入 self 参数。

代码定义

Example1

_Tab.BasicFunc()

--得到结果
--I'm a BasicFunc
  • 只是普通的调用方法打印一句话

Example2

_Tab.FuncWithSelf(_Tab)
--得到结果
--[[
    FuncWithSelf _Tab 
    table: 006B97C0
    FuncWithSelf selfTable 
    table: 006B97C0
--]]
  • 此处传入自身_Tab给FixTableFunc这个方法
  • 局部变量selfTab和 _Tab同时指向 _Tab的指针,如果改了selfTab, _Tab 也会变

Example3

_Tab:FuncWithSelf()
--[[
    FuncWithSelf _Tab 
    table: 00F19680
    FuncWithSelf selfTable 
    table: 00F19680
--]]
  • Tab.FuncWithSelf( _Tab ) == _Tab:FuncWithSelf()
  • 因为冒号调用方法时,会默认把自身传递进去

Example4

_Tab.FuncWithSelfArg(_Tab,12)

--打印结果是
--[[
    _Tab
    table: 007F9748
    FuncWithSelfArg selfTable 
    table: 007F9748
    FuncWithSelfArg otherArg 
    12  
--]]
  • _Tab和selfTable的内存地址是一样的,otherArg = 12

Example5

_Tab:FuncWithSelfArg(12)
--[[
    _Tab
    table: 00F698D8
    FuncWithSelfArg selfTable 
    table: 00F698D8
    FuncWithSelfArg otherArg 
    12
--]]
  • Tab和selfTable内存地址相等,冒号方法默认传入 _Tab给FuncWithSelfArg方法
  • 然后把12赋值给otherArg

Example6

_Tab.ColonFuncNoParam(_Tab)

--[[
    ColonFuncNoParam _Tab 
    table: 00C49978
    ColonFuncNoParam self 
    table: 00C49978
--]]

_Tab.ColonFuncNoParam()
--[[  
    ColonFuncNoParam _Tab 
    table: 00B298B0
    ColonFuncNoParam self 
    nil
--]]

  • 用点来访问一个冒号方法,必须传入自身
  • 如果不传则self为空,则不能通过self修改自身

Example7

_Tab:ColonFuncNoParam()

--[[  
    ColonFuncNoParam _Tab 
    table: 01039798
    ColonFuncNoParam self 
    table: 01039798
--]]
  • 通过冒号去调用一个冒号方法,默认就传入了self,所以可以直接用self修改自身
  • self == _Tab

Example8

_Tab.ColonFuncWithParam(_Tab,12)

--[[
    ColonFuncWithParam _Tab 
    table: 001F95B8
    ColonFuncWithParam self 
    table: 001F95B8
    ColonFuncWithParam arg 
    12
--]]

Example9

_Tab:ColonFuncWithParam(12)

--[[  
    ColonFuncWithParam _Tab 
    table: 00FF98D8
    ColonFuncWithParam self 
    table: 00FF98D8
    ColonFuncWithParam arg 
    12
--]]
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • importUIKit classViewController:UITabBarController{ enumD...
    明哥_Young阅读 3,891评论 1 10
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,981评论 19 139
  • linux资料总章2.1 1.0写的不好抱歉 但是2.0已经改了很多 但是错误还是无法避免 以后资料会慢慢更新 大...
    数据革命阅读 12,239评论 2 33
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,216评论 30 472
  • 基础1.r''表示''内部的字符串默认不转义2.'''...'''表示多行内容3. 布尔值:True、False(...
    neo已经被使用阅读 1,731评论 0 5