lua的:和.的区别(语法糖)

	Account = {balance = 1000}    -- 账户余额初始为1000。

	----【定义函数】
	--[[ 取钱。
		 使用面向对象的方式隐藏了"self"参数,
		 "withdraw()"完整的参数列表是"Account.withdraw(self, v)"。]]
	function Account:withdraw(v)
		self.balance = self.balance - v
	end


	----【调用函数】
	--[[ 使用面向对象的方式隐藏了"self"参数,
		 实际传递给"withdraw()"的参数是"Account"和"100.00"。]]
	Account:withdraw(100.00)
	print(Account.balance)    --> 900.0




	Account = {balance = 1000}    -- 账户余额初始为1000。

	function Account.withdraw(self, v)    -- 使用"."定义函数。
		self.balance = self.balance - v
	end


	Account:withdraw(100.00)        -- 使用":"调用函数。
	print(Account.balance)          -- 900.0
	 
	-- 存钱。
	function Account:deposit(v)      -- 使用":"定义函数。
	  self.balance = self.balance + v
	end
	Account.deposit(Account, 600.00)    -- 使用"."调用函数。
	print(Account.balance)             -- 1500.0

  1. 总结

  用冒号(:)调用函数时,会默认传一个值(调用者自身self)作为第一个参数;
       用点(.)调用函数时,则没有
    
  用冒号(:)创建函数时,会默认添加一个参数self,作为第一个参数;
       用点(.)调用函数时,则没有
    
  . 和 :函数可以相互调用,但需要正确的传递self值。
]]--

 

原文地址:http://www.godhasbug.com/unity-lua-yufatang/

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值