zoukankan      html  css  js  c++  java
  • Lua/luajit 点与冒号的区别

    local _Tab = {[1] = "Hello Lua",x = 10}
    
    --通过点调用一个普通的方法
    function _Tab.BasicFunc()
        print("I'm a BasicFunc 
    ")
    end
    
    _Tab.BasicFunc()
    
    --通过点来调用并且传递一个自身
    function _Tab.FuncWithSelf(selfTable)
        print("FuncWithSelf".." _Tab ")
        print(_Tab)
    
        print("FuncWithSelf".." selfTable ")
        print(selfTable , "
    ")    
    end
    
    _Tab.FuncWithSelf(_Tab)
    --通过点来调用,传递一个自身并且再传递一个值
    function _Tab.FuncWithSelfArg(selfTable,otherArg)
        print("_Tab")
        print(_Tab)
    
        print("FuncWithSelfArg".." selfTable ")
        print(selfTable )
    
        print("FuncWithSelfArg".." otherArg ")
        print(otherArg, "
    ")
    end
    
    _Tab.FuncWithSelfArg(_Tab, 12)
    _Tab:FuncWithSelfArg(12)
    --通过冒号来实现一个无参数方法
    function _Tab:ColonFuncNoParam()
        print("ColonFuncNoParam".." _Tab ")
        print(_Tab)
    
        print("ColonFuncNoParam".." self ")
        print(self,"
    ")
    end
    _Tab.ColonFuncNoParam(_Tab)
    _Tab.ColonFuncNoParam()
    _Tab:ColonFuncNoParam()
    --通过冒号来实现一个有参数的方法
    function _Tab:ColonFuncWithParam(arg)
        print("ColonFuncWithParam".." self ")
        print(self)
        
        print("ColonFuncWithParam".." arg ")
        print(arg,"
    ")
    end
    
    
    _Tab.ColonFuncWithParam(12)
    _Tab.ColonFuncWithParam(_Tab,12)
    _Tab:ColonFuncWithParam(12)

    输出:

    I'm a BasicFunc 
    
    FuncWithSelf _Tab 
    table: 0x7fe94ec09c70
    FuncWithSelf selfTable 
    table: 0x7fe94ec09c70    
    
    _Tab
    table: 0x7fe94ec09c70
    FuncWithSelfArg selfTable 
    table: 0x7fe94ec09c70
    FuncWithSelfArg otherArg 
    12    
    
    _Tab
    table: 0x7fe94ec09c70
    FuncWithSelfArg selfTable 
    table: 0x7fe94ec09c70
    FuncWithSelfArg otherArg 
    12    
    
    ColonFuncNoParam _Tab 
    table: 0x7fe94ec09c70
    ColonFuncNoParam self 
    table: 0x7fe94ec09c70    
    
    ColonFuncNoParam _Tab 
    table: 0x7fe94ec09c70
    ColonFuncNoParam self 
    nil    
    
    ColonFuncNoParam _Tab 
    table: 0x7fe94ec09c70
    ColonFuncNoParam self 
    table: 0x7fe94ec09c70    
    
    ColonFuncWithParam self 
    12
    ColonFuncWithParam arg 
    nil    
    
    ColonFuncWithParam self 
    table: 0x7fe94ec09c70
    ColonFuncWithParam arg 
    12    
    
    ColonFuncWithParam self 
    table: 0x7fe94ec09c70
    ColonFuncWithParam arg 
    12    

    总结 :

    无论在定义method或 function时用的点号, 还是冒号,  Lua/Luajit 中最终是以使用时的点号或冒号为准, 并且Object.method(self, arg...) 相当于 Object:method(arg...), 即冒号会自动传入self 到第一个参数, 这样显得更简洁优雅. 

  • 相关阅读:
    ubuntu16.04添加开机启动任务
    Elasticsearch-5.0.0移植到ubuntu16.04
    转:解决npm install慢的问题
    解决virtualbox装ghost xp装驱动时报portcls.sys蓝屏的问题
    git文件迁移到新架构
    ubuntu16.04文件形式安装mongodb
    linux启动流程
    启动WAMPSERVER出现计算机中丢失 MSVCR110.dll
    socket编程的网络协议
    PHP7的新特性
  • 原文地址:https://www.cnblogs.com/sunsky303/p/13523941.html
Copyright © 2011-2022 走看看