欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > Lua数据类型

Lua数据类型

2025/2/25 21:53:25 来源:https://blog.csdn.net/gopher9511/article/details/143579638  浏览:    关键词:Lua数据类型

Lua 语言

数据类型

Lua 有以下数据类型:

  • nil:表示一个无效值,相当于 NULL。
  • boolean:true 或 false。
  • number:整数或浮点数。
  • string:字符串。
  • function:函数。
  • userdata:用户数据。
  • thread:线程。
  • table:表。

nil

nil 表示一个无效值,相当于 NULL。
一个变量在第一次赋值前的默认值是 nil。

local x = nil
print(x) -- nil
print(nil) -- nil

boolean

boolean 类型只有 true 和 false 两个值。
Lua 中 nil 和 false 为"假",其它所有值均为"真"。比如 0 和空字符串(“”) 就是"真" 。

print(true) -- true
print(false) -- false
print(nil) -- nil
print(123) -- true

number

number 类型可以表示整数或浮点数。
可以使用数学函数 math。
floor(向下取整) 和 math.ceil(向上取整) 进行取整操作

底层实现:一般地,Lua 的 number 类型就是 C/C++中的 long long int 或 double
类型来实现的 ( Standard Lua uses 64-bit integers and double-precision ( 64-bit) floats )。
语法结构的简单是付出空间代价换来的。

print(123) -- 123
print(3.14) -- 3.14

string

string 类型用来表示文本。
使用一对匹配的单引号。
Lua 中没有字符类型,所谓的字符类型也是含有一个字符的字符串而己。
字符串还可以用一种长括号(即[[ ]]) 括起来的方式定义。其好处就在于: 1 -> 转义
并不展开(此举,跟 C++11 中 R"(hello lua)"相似) , 2 -> 便于多行书写。
Lua 中的字符串没有[]下标的概念。

[=[ ]=]等号两侧不可以用空格,主要起到层级/配对的作用。

print("Hello, world!") -- Hello, world!
print('Hello, world!') -- Hello, world!
print([[Hello,
world!]]) -- Hello, world!print("Hello" .. "world!") -- Helloworld!
print("Hello" .. " " .. "world!") -- Hello world!

function

函数,在 Lua 中也是一种数据类型
function 类型用来表示函数。
函数可以存储在变量中,可以通过作参数传递给其他函数,还可以作为其他函数的返
回值。其上行为同普通变量无异

function add(a, b)return a + b
endprint(add(1, 2)) -- 3

有名函数的定义本质上是:匿名函数对变量的赋值。为说明这一点,其如下写法也是
合法的。

function foo()
end
等价于
foo = function ()
end
类似的
local function foo()
end
等价于
local foo = function ()
end

函数入参无类型/返值无限制

function hello(a, b)
print("a:", type(a), "b:", type(b))
print(a, b)
return 1, 2, 3, 4, 5
end

userdata

userdata 类型用来表示用户数据。

-- 创建一个 userdata
local obj = {}-- 给 userdata 设置元表
setmetatable(obj, {__add = function(a, b)return a.value + b.valueend
})-- 设置 userdata 的值
obj.value = 10-- 调用 userdata 的元方法
print(obj + obj) -- 20

thread

thread 类型用来表示线程。

-- 创建一个线程
local thread = coroutine.create(function()for i = 1, 10 doprint(i)coroutine.yield()end
end)-- 启动线程
coroutine.resume(thread)-- 等待线程结束
coroutine.status(thread) -- dead

table

Table 类型中一种基于 k-v 类型,实现了一种抽象的 “map<k,v>”。"map<k,v>"是
一种具有特殊索引方式的数组

table 类型用来表示表。

索引 k 通常是字符串(string) 或者 number 类型,但也可以是除 nil 以外的任意类型
的值。
v 则可以是 lua 中的任意类型

对于无 key 的类型,此时的 key 类型为 number,下标从 1 开始,下标依次累加。

-- 创建一个空表
local t = {}-- 给表设置字段
t.name = "Alice"
t.age = 25-- 访问表的字段
print(t.name) -- Alice
print(t.age) -- 25-- 创建一个有初始值的表
local t2 = {name = "Bob", age = 30}-- 遍历表
for k, v in pairs(t2) doprint(k, v)
end-- 输出:
-- name Bob
-- age 30local arr = {1, 2, 3, 4, 5, 6, 7, 8}
for i = 1, 8 do
print(arr[i])
end
local corp = {
web = "www.edu.nzhsoft.cn", -- k = "web"
-- v = "edu.nzsoft.cn"
telephone = "17090078295", -- k = "telephone"
-- v = "17090078295"
staff = {"jack","Scott","Gary"},
10086, -- key = 1 v = 10086
10010, -- key = 2 v = 10010
[10] = 160, -- key = 10 v = 160
["city"] = "guangzhou"
}
print(corp.web) -->output:edu.nzsoft.cn
print(corp["telephone"]) -->output:17090078295
print(corp[2]) -->output:100191
print(corp["city"]) -->output:"Beijing"
print(corp.staff[1]) -->output:Jack
print(corp[10]) -->output:360

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词