先来看一段简单的代码:

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()
Animal.Sleep()

输出结果为:

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

由此可见,
定义:
在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

Lua-面向对象中函数使用时冒号(:)和点(.)的区别的更多相关文章

  1. getContext在谷歌浏览器中,使用时要先加载canvas对象,否则会提示'getContext is null'

    <body> <canvas id=" style="border:1px solid #c3c3c3;"> Your browser does ...

  2. time.h文件中包含的几个函数使用时须注意事项

    time.h头文件中包含以下函数 char* asctime(const struct tm *tm); char* asctime_r(const struct tm *tm,char *buf); ...

  3. 三种语言(c++、as、lua)中函数的差异性

    对于不同的语言, 尤其是静态语言和动态语言, 对于函数的定义(即如何看待一个函数)和处理截然不同.具体来说可以分为两类: 1.将函数视为第一类型值, 即函数和其他的对象一样, 都是语言中一个普通的对象 ...

  4. 理解lua 语言中的点、冒号与self

    转载自: http://blog.csdn.net/wangbin_jxust/article/details/12170233 lua编程中,经常遇到函数的定义和调用,有时候用点号调用,有时候用冒号 ...

  5. free函数使用时的注意事项。

    free函数是我们在写C语言程序时常用的函数,但是使用时需要注意,一不小心很肯能会引起吐核. 注意:free函数与malloc()函数配对使用,malloc函数释放申请的动态内存.对于free(p)这 ...

  6. LUA table中函数的调用

    1 lua中函数作为表中元素时有三种定义方式 采用‘:’来定义,实际上隐藏了一个形参的声明,这个形参会截获调用函数时的第一个实参并把它赋值给self 2 调用方式,点号和冒号 functb:hello ...

  7. Netty4.0.24.Final 版本中 IdleStateHandler 使用时的局限性

    使用Netty在客户端和服务端建立通讯通道,一般来说,一个连接可能很久没有访问,由于各种各样的网络问题导致连接已经失效,客户端再次发送请求时会产生连接异常. 基于这个原因,需要在客户端和服务端之间建立 ...

  8. Entity framework 中Where、First、Count等查询函数使用时要注意

    在.Net开发中,Entity framework是微软ORM架构的最佳官方工具.我们可以使用Lambda表达式在Entity framework中DbSet<T>类上直接做查询(比如使用 ...

  9. js中函数调用时,对参数个数和类型没有要求

    因为js是一种弱类型的编程语言,对数据类型的要求没有其他编程语言的要求严格,所以在定义函数的时候不需要像java一样对其传入参数的类型进行定,也对传入参数的个数没有要求. js函数的参数与大多数其他语 ...

随机推荐

  1. 24.编写一个Car类,具有String类型的属性品牌,具有功能drive; 定义其子类Aodi和Benchi,具有属性:价格、型号;具有功能:变速; 定义主类E,在其main方法中分别创建Aodi和Benchi的对象并测试对象的特 性。

    package zhongqiuzuoye; public class Car { String brand; public void drive() {} } package zhongqiuzuo ...

  2. Codeforces Round #380 (Div. 2) 总结分享

    B. Spotlights 题意 有n×m个格子的矩形舞台,每个格子里面可以安排一个演员或聚光灯,聚光灯仅可照射一个方向(俯视,上下左右).若聚光灯能照到演员,则称为"good positi ...

  3. win环境安装python爬虫框架scrapy

    #官网下载python for windows #https://www.python.org/downloads/ #安装后在“计算机->属性->高级系统设置->环境变量-> ...

  4. JS原生第二篇 (帅哥)

    1.1 Javascript 作用  1.  网页特效 2. 用户交互 3. 表单验证 Js  就是可以用来控制   结构  和 样式 . 1.2  体验js   认识常用的三个输出语句.  都属于 ...

  5. Warning: Null value is eliminated by an aggregate or other SET operation.

    Null 值会被聚合函数忽略,默认情况下,Sql Server会给出Warning: Warning: Null value is eliminated by an aggregate or othe ...

  6. Oracle 11g系列:数据库

    1.创建Oracle数据库 创建Oracle数据库的最常用工具为Database Configuration Assistant(数据库配置助手),依次选择[开始]|[所有程序]|[Oracle-Or ...

  7. maven+svn忽略提交到svn的文件

  8. python--基础学习(六)sqlite数据库基本操作

    python系列均基于python3.4环境 1.新建数据表 新建表,命名为student(id, name, score, sex, age),id为关键字,代码如下: import sqlite3 ...

  9. 【原创】C#搭建足球赛事资料库与预测平台(6) 赔率数据表设计2

            本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新 开源C#彩票数据资料库系列文章总目录:[目录]C#搭建足球赛事资料库与预测平台与彩票数据分析目录 本篇文章开始将逐步介 ...

  10. spring源码分析之spring jmx

    JMX架构定义: https://docs.oracle.com/javase/8/docs/technotes/guides/jmx/overview/architecture.html Archi ...