Lua學習筆記
文章目录
關於Lua的官方定義:
Lua is a powerful, fast, lightweight, embeddable scripting language.
Lua combines simple procedural syntax with powerful data description constructs based on associative arrays and extensible semantics. Lua is dynamically typed, runs by interpreting bytecode for a register-based virtual machine, and has automatic memory management with incremental garbage collection, making it ideal for configuration, scripting, and rapid prototyping.
別的我就不多安利了
type
table
table是類似class的hash_map,由data和method兩部分組成。簡而言之,數據和行爲是分離的,後者在Lua中稱爲metatable。
metatable有很多特有的鍵,在此我們粗暴的認爲特有鍵類似c++中的操作符,它們的值一般爲function或table。
setmetatable
我們可以通過setmetatable來賦予table不同的動作。
1 | t = {} -- 普通的table |
以上代碼可以縮略爲:1
t = setmetatable({}, {})
__index與__newindex
1 | other = {} |
__index
類似於[]
操作符__newindex
類似於[]=
操作符
1 | t = setmetatable({}, { |
rawget
和rawset
可以避免調用重載後的操作符(__index和__newindex)
__tostring
1 | t = setmetatable({ 1, 2, 3 }, { |
operator
運算符只能是函數
- __add
- __sub
- __mul
- __div
- __mod
- __unm
- __concat
- __eq
- __lt
- __le
function
一個機智的計數器
1 | function InitCounter() |
wheel
德堪邀請我入坑,考慮了一下還是可行的。
現在手頭還沒拿到項目,所以自己造輪子玩。