Lua點(diǎn)與冒號的區(qū)別

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

--通過點(diǎn)調(diào)用一個(gè)普通的方法
function _Tab.BasicFunc()
    print("I'm a BasicFunc")
end

--通過點(diǎn)來調(diào)用并且傳遞一個(gè)自身
function _Tab.FuncWithSelf(selfTable)
    print("FuncWithSelf".." _Tab ")
    print(_Tab)

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

--通過點(diǎn)來調(diào)用,傳遞一個(gè)自身并且再傳遞一個(gè)值
function _Tab.FuncWithSelfArg(selfTable,otherArg)
    print("_Tab")
    print(_Tab)

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

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

--通過冒號來實(shí)現(xiàn)一個(gè)無參數(shù)方法
function _Tab:ColonFuncNoParam()
    print("ColonFuncNoParam".." _Tab ")
    print(_Tab)

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

--通過冒號來實(shí)現(xiàn)一個(gè)有參數(shù)的方法
function _Tab:ColonFuncWithParam(arg)
    print("ColonFuncWithParam".." self ")
    print(self)
    
    print("ColonFuncWithParam".." arg ")
    print(arg)
end

Lua方法調(diào)用. :

方法的使用

  • 冒號操作會(huì)帶入一個(gè) self 參數(shù),用來代表 自己。而點(diǎn)號操作,只是 內(nèi)容 的展開。
  • 在函數(shù)定義時(shí),使用冒號將默認(rèn)接收一個(gè) self參數(shù),而使用點(diǎn)號則需要顯式傳入 self 參數(shù)。

代碼定義

Example1

_Tab.BasicFunc()

--得到結(jié)果
--I'm a BasicFunc
  • 只是普通的調(diào)用方法打印一句話

Example2

_Tab.FuncWithSelf(_Tab)
--得到結(jié)果
--[[
    FuncWithSelf _Tab 
    table: 006B97C0
    FuncWithSelf selfTable 
    table: 006B97C0
--]]
  • 此處傳入自身_Tab給FixTableFunc這個(gè)方法
  • 局部變量selfTab和 _Tab同時(shí)指向 _Tab的指針,如果改了selfTab, _Tab 也會(huì)變

Example3

_Tab:FuncWithSelf()
--[[
    FuncWithSelf _Tab 
    table: 00F19680
    FuncWithSelf selfTable 
    table: 00F19680
--]]
  • Tab.FuncWithSelf( _Tab ) == _Tab:FuncWithSelf()
  • 因?yàn)槊疤栒{(diào)用方法時(shí),會(huì)默認(rèn)把自身傳遞進(jìn)去

Example4

_Tab.FuncWithSelfArg(_Tab,12)

--打印結(jié)果是
--[[
    _Tab
    table: 007F9748
    FuncWithSelfArg selfTable 
    table: 007F9748
    FuncWithSelfArg otherArg 
    12  
--]]
  • _Tab和selfTable的內(nèi)存地址是一樣的,otherArg = 12

Example5

_Tab:FuncWithSelfArg(12)
--[[
    _Tab
    table: 00F698D8
    FuncWithSelfArg selfTable 
    table: 00F698D8
    FuncWithSelfArg otherArg 
    12
--]]
  • Tab和selfTable內(nèi)存地址相等,冒號方法默認(rèn)傳入 _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
--]]

  • 用點(diǎn)來訪問一個(gè)冒號方法,必須傳入自身
  • 如果不傳則self為空,則不能通過self修改自身

Example7

_Tab:ColonFuncNoParam()

--[[  
    ColonFuncNoParam _Tab 
    table: 01039798
    ColonFuncNoParam self 
    table: 01039798
--]]
  • 通過冒號去調(diào)用一個(gè)冒號方法,默認(rèn)就傳入了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
--]]
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • importUIKit classViewController:UITabBarController{ enumD...
    明哥_Young閱讀 3,880評論 1 10
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,923評論 18 139
  • linux資料總章2.1 1.0寫的不好抱歉 但是2.0已經(jīng)改了很多 但是錯(cuò)誤還是無法避免 以后資料會(huì)慢慢更新 大...
    數(shù)據(jù)革命閱讀 12,218評論 2 33
  • *面試心聲:其實(shí)這些題本人都沒怎么背,但是在上海 兩周半 面了大約10家 收到差不多3個(gè)offer,總結(jié)起來就是把...
    Dove_iOS閱讀 27,210評論 30 471
  • 基礎(chǔ)1.r''表示''內(nèi)部的字符串默認(rèn)不轉(zhuǎn)義2.'''...'''表示多行內(nèi)容3. 布爾值:True、False(...
    neo已經(jīng)被使用閱讀 1,719評論 0 5