• Lua-面向对象中函数使用时冒号(:)和点(.)的区别


    原文:http://www.cnblogs.com/zzya/p/5778119.html

    Lua-面向对象中函数使用时冒号(:)和点(.)的区别

    local Animal = {}
    
    function Animal:Eat( food )
        print("Animal:Eat", self, food)
    end
    
    function Animal.Sleep( time )
        print("Animal.Sleep", self, time)
    end
    
    Animal:Eat("grass")
    Animal.Eat("grass")
    Animal:Sleep(1)
    Animal.Sleep(1)
    

      

    输出结果为:

    Animal:Eat table: 0x7f8421c07540 grass
    Animal:Eat    grass    nil
    Animal.Sleep    nil    table: 0x7f8421c07540
    Animal.Sleep    nil    1

    由此可见,
    定义:
    在Eat(冒号函数)内部有一个参数self,在Sleep(点函数)内部没有参数self;
    调用:
    用冒号(:)调用函数时,会默认传一个值(调用者自身)作为第一个参数;
    用点(.)调用函数时,则没有;

    -- 如果要使结果一致,则:

    Animal:Eat("grass")
    Animal.Eat(Animal,"grass")
    Animal:Sleep()
    Animal.Sleep(Animal)

    输出结果:

    Animal:Eat    table: 0x7f8421c07540    grass
    Animal:Eat    table: 0x7f8421c07540    grass
    Animal.Sleep    nil    table: 0x7f8421c07540
    Animal.Sleep    nil    table: 0x7f8421c07540

    -- 我们为什么可以用.和:来定义函数
    function Animal.Sleep( time ) end
    -- 这种写法是一种语法糖(syntactic sugar),它的原型是:
    Animal.Sleep = function ( time ) end

    用双冒号(:)时,也是一种语法糖,实际上默认传递一个self(Animal)参数:
    function Animal:Eat( food ) end
    等价于
    function Animal.Eat( self, food ) end

    可参考Lua函数定义:

    http://www.lua.org/manual/5.2/manual.html#pdf-next

    3.4.10 – Function Definitions

    The syntax for function definition is

    	functiondef ::= function funcbody
    	funcbody ::= ‘(’ [parlist] ‘)’ block end
    

    The following syntactic sugar simplifies function definitions:

    	stat ::= function funcname funcbody
    	stat ::= local function Name funcbody
    	funcname ::= Name {‘.’ Name} [‘:’ Name]
    

    The statement

         function f () body end
    

    translates to

         f = function () body end
    

    The statement

         function t.a.b.c.f () body end
    

    translates to

         t.a.b.c.f = function () body end
    

    The statement

         local function f () body end
    

    translates to

         local f; f = function () body end
    

    not to

         local f = function () body end
  • 相关阅读:
    U盘启动盘恢复为普通盘
    TP框架模板中ifelse
    TP框架中多条件筛选
    日期选择器:jquery datepicker的使用
    配置本地环境,让内网可访问
    FlexSlider插件的详细设置参数
    CentOS+Nginx+PHP+MySQL详细配置(图解)
    Linux 服务器环境启动
    javascript自定义浏览器右键菜单
    强大实用的jQuery幻灯片插件Owl Carousel
  • 原文地址:https://www.cnblogs.com/vsirWaiter/p/7514272.html
Copyright © 2020-2023  润新知