:
<pre name="code" class="cpp">--lua 中点(.)和冒号(:)区别 class = {} function class:new(x, y) local mt = {} mt.x = x mt.y = y self.__index = self setmetatable(mt, self) return mt end -- 等效 function class.printf(self) function class:printf() print(self.x ,self.y) end different = class:new(1,2) different是类new的一个实例-- different.printf() 这个不对 函数内找不到self -- different:printf() --正确 默认形参是self different.printf(different) --正确 没有默认形参 需手动添加在函数中需要用到的参数 --规则: 在使用的时候 属性用点 函数用冒号可以避免混乱