:
<pre name="code" class="cpp">--lua 中點(.)和冒號(:)區(qū)別
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() 這個不對 函數(shù)內(nèi)找不到self
-- different:printf() --正確 默認形參是self
different.printf(different) --正確 沒有默認形參 需手動添加在函數(shù)中需要用到的參數(shù) --規(guī)則: 在使用的時候 屬性用點 函數(shù)用冒號可以避免混亂
?
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
- 上一篇: 人越过越麻木
- 下一篇: xampp 下安装mysql-pytho