美文网首页
Lua点与冒号的区别

Lua点与冒号的区别

作者: YellowFish | 来源:发表于2017-08-19 16:27 被阅读0次
    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
    --]]
    

    相关文章

      网友评论

          本文标题:Lua点与冒号的区别

          本文链接:https://www.haomeiwen.com/subject/atitdxtx.html