[lua]变量与数据类型
本帖最后由 hsx8998283 于 2026-5-25 13:57 编辑变量声明与赋值全局变量(不推荐):n = 100name = "张三"
局部变量(推荐 ✅):local age = 18
local isStudent = true
4.2 Lua的8种数据类型
类型描述示例
nil空值,表示无效值local a = nil
number数字(整数和浮点数)local n = 100
string字符串local s = "hello"
boolean布尔(true/false)local b = true
function函数local f = function() end
table表(核心数据结构)local t = {}
userdataC数据结构-
thread协程-
4.3 type函数(检测数据类型)
print(type(100))
-- 输出:numberprint(type("hello"))
-- 输出:stringprint(type(true))
-- 输出:booleanprint(type(nil))
-- 输出:nilprint(type({}))
-- 输出:tableprint(type(print))
-- 输出:function
4.4 变量命名规则必须遵守:
[*]必须以字母或下划线开头
[*]后续可以是字母、数字或下划线
[*]区分大小写
[*]不能使用保留关键字
4.5 Lua保留关键字(不能用作变量名)
and break do else elseifend
false for function ifin local
nil not orrepeat return
then true untilwhile goto
变量与数据类型 看帖回复事一种美德
页:
[1]