前言
这里只是简单列出Lua语言的知识点
一、基础语法
1.1 注释
1.2 变量声明
1 2 3
| a = 10 local b = 20 c, d = 30, "Lua"
|
1.3 数据类型
| 类型 |
说明 |
示例 |
nil |
空值 |
local x = nil |
boolean |
布尔值 |
true, false |
number |
数字 |
3.14, 42 |
string |
字符串 |
"Hello", 'World' |
table |
表/数组 |
{1, 2, 3} |
function |
函数 |
function() end |
thread |
协程 |
coroutine.create() |
userdata |
自定义数据 |
用于C交互 |
二、运算符
2.1 算术运算符
2.2 关系运算符
2.3 逻辑运算符
2.4 字符串连接
1
| local s = "Hello" .. " " .. "Lua!"
|
2.5 长度运算符
1 2 3
| local len = #"Lua" local tbl = {1,2,3} print(#tbl)
|
三、控制结构
3.1 条件语句
1 2 3 4 5 6 7
| if score >= 90 then print("A") elseif score >= 80 then print("B") else print("C") end
|
3.2 循环语句
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| local i = 1 while i <= 5 do print(i) i = i + 1 end
for i = 1, 5 do print(i) end
for i = 10, 1, -2 do print(i) end
local j = 1 repeat print(j) j = j + 1 until j > 5
|
四、函数
4.1 函数定义
1 2 3 4 5 6 7 8
| function add(a, b) return a + b end
local multiply = function(a, b) return a * b end
|
4.2 多重返回值
1 2 3 4 5
| function getUser() return "Alice", 30 end
local name, age = getUser()
|
4.3 变长参数
1 2 3 4 5 6 7 8 9
| function sum(...) local total = 0 for _, v in ipairs({...}) do total = total + v end return total end
print(sum(1,2,3,4))
|
五、表(Table)
5.1 基本操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| local arr = {10, 20, 30} print(arr[1])
local person = { name = "Bob", age = 25, sayHello = function() print("Hello!") end }
print(person.name) person.sayHello()
|
5.2 表操作函数
1 2 3 4 5
| local t = {3, 1, 4}
table.insert(t, 2, 5) table.remove(t, 3) table.sort(t)
|
6.1 元表基础
1 2 3 4 5 6 7 8 9
| local t1 = {10, 20} local mt = { __index = function(t, k) return "Key not found: " .. k end }
setmetatable(t1, mt) print(t1[3])
|
6.2 运算符重载
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| local v1 = {x=1, y=2} local v2 = {x=3, y=4}
local mt = { __add = function(a, b) return {x = a.x + b.x, y = a.y + b.y} end }
setmetatable(v1, mt) setmetatable(v2, mt)
local v3 = v1 + v2 print(v3.x, v3.y)
|
七、模块系统
7.1 创建模块
1 2 3 4 5 6 7 8 9 10 11 12
| local M = {}
function M.add(a, b) return a + b end
function M.subtract(a, b) return a - b end
return M
|
7.2 使用模块
1 2
| local math = require "mymodule" print(math.add(5, 3))
|
八、协程(Coroutines)
8.1 协程基础
1 2 3 4 5 6 7 8
| local co = coroutine.create(function() print("Start") coroutine.yield() print("End") end)
coroutine.resume(co) coroutine.resume(co)
|
8.2 生产者-消费者模式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| function producer() local i = 0 return function() i = i + 1 return i end end
function consumer(prod) while true do local value = prod() print("Consumed:", value) coroutine.yield() end end
local p = producer() local co = coroutine.create(consumer, p)
for i = 1, 5 do coroutine.resume(co) end
|
九、文件操作
9.1 文件读写
1 2 3 4 5 6 7 8 9 10
| local file = io.open("test.txt", "w") file:write("Hello Lua!\n") file:close()
local file = io.open("test.txt", "r") local content = file:read("*a") print(content) file:close()
|
十、错误处理
10.1 pcall 和 xpcall
1 2 3 4 5 6 7
| local success, result = pcall(function() error("Something went wrong") end)
if not success then print("Error:", result) end
|
10.2 assert
1 2 3
| local value = -5 assert(value >= 0, "Value must be non-negative")
|
十一、标准库
11.1 常用模块
| 模块 |
用途 |
string |
字符串处理 |
table |
表操作 |
math |
数学函数 |
io |
输入输出 |
os |
操作系统接口 |
debug |
调试工具 |
coroutine |
协程支持 |
package |
模块管理 |
11.2 常用函数
1 2 3 4 5 6 7
| print("Hello") type(42) tonumber("123") tostring(123) pcall(func) next(t) collectgarbage()
|
十二、最佳实践
- 使用局部变量:默认使用
local 声明变量
- 避免全局污染:使用模块组织代码
- 正确处理nil:避免对nil值进行操作
- 利用元表:实现面向对象和运算符重载
- 合理使用协程:实现协作式多任务
- 资源管理:及时关闭文件和释放资源
- 错误处理:使用pcall保护关键代码
- 表设计:区分数组部分和哈希部分
- 性能优化:避免在循环中创建表
- 代码规范:保持一致的命名和缩进风格
版权声明: 此文章版权归曦曦所有,如有转载,请注明来自原作者