跳轉到內容

Lua 程式設計/冒號

來自華夏公益教科書,開放的書籍,為開放的世界

方法定義

[編輯 | 編輯原始碼]

冒號 符號 可用於定義 方法

-- These two statements are equivalent
function a.b:c (params) body end
a.b.c = function (self, params) body end

面向物件方法呼叫

[編輯 | 編輯原始碼]

冒號 符號面向物件 呼叫提供了一種特殊的 語法

-- By using a colon operator, the name of the object does not need to be passed as a first argument
myobj:foo(n)    -- equivalent to  myobj.foo(myobj, n)

例項方法呼叫

[編輯 | 編輯原始碼]

冒號符號用於定義對例項方法的呼叫。

華夏公益教科書