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

 

posted @ 2016-08-19 22:08  zzya  阅读(2585)  评论(0编辑  收藏  举报