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
end

print(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.value
  end
})

-- 设置 userdata 的值
obj.value = 10

-- 调用 userdata 的元方法
print(obj + obj) -- 20

thread

thread 类型用来表示线程。

-- 创建一个线程
local thread = coroutine.create(function()
  for i = 1, 10 do
    print(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) do
  print(k, v)
end

-- 输出:
-- name Bob
-- age 30



local 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

可能只会写BUG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值