|
|
本帖最后由 hsx8998283 于 2026-5-25 13:57 编辑
变量声明与赋值全局变量(不推荐): n = 100name = "张三"
局部变量(推荐 ✅): local age = 18
local isStudent = true
4.2 Lua的8种数据类型[td]| 类型 | 描述 | 示例 | | 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 = {} | | userdata | C数据结构 | - | | 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
|
|