Lua点与冒号的区别

2017-08-19  本文已影响0人  YellowFish
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方法调用. :

方法的使用

代码定义

Example1

_Tab.BasicFunc()

--得到结果
--I'm a BasicFunc

Example2

_Tab.FuncWithSelf(_Tab)
--得到结果
--[[
    FuncWithSelf _Tab 
    table: 006B97C0
    FuncWithSelf selfTable 
    table: 006B97C0
--]]

Example3

_Tab:FuncWithSelf()
--[[
    FuncWithSelf _Tab 
    table: 00F19680
    FuncWithSelf selfTable 
    table: 00F19680
--]]

Example4

_Tab.FuncWithSelfArg(_Tab,12)

--打印结果是
--[[
    _Tab
    table: 007F9748
    FuncWithSelfArg selfTable 
    table: 007F9748
    FuncWithSelfArg otherArg 
    12  
--]]

Example5

_Tab:FuncWithSelfArg(12)
--[[
    _Tab
    table: 00F698D8
    FuncWithSelfArg selfTable 
    table: 00F698D8
    FuncWithSelfArg otherArg 
    12
--]]

Example6

_Tab.ColonFuncNoParam(_Tab)

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

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

Example7

_Tab:ColonFuncNoParam()

--[[  
    ColonFuncNoParam _Tab 
    table: 01039798
    ColonFuncNoParam self 
    table: 01039798
--]]

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
--]]
上一篇 下一篇

猜你喜欢

热点阅读