Lua 类型教程

lua-users home
wiki

这是对 Lua 中八种值类型的简短介绍:数字(number)、字符串(string)、布尔(boolean)、表(table)、函数(function)、nil、用户数据(userdata)、线程(thread)。每个部分介绍一种不同的类型。

请参考 TutorialExamples 来了解如何运行这里提供的示例。我们将使用 print() 函数来输出值或对这些值进行的计算。参数周围的括号很重要,省略它们会导致错误。

> print("hello") -- print the string hello.
hello

数字 (Numbers)

数字类型表示浮点数(带小数的数)。自 5.3 版本起,整数(无小数的数)有了单独的内部表示。但是,它们也被认为是“数字”类型。

Lua 允许使用常规的运算符对数字进行简单的算术运算,例如加、减、乘和除。

> print(2+2)
4
> print(2-7)
-5
> print(7*8)
56
> print(7/8)
0.875

请注意,数字没有被四舍五入成整数。它们是浮点数,也就是实数。我们可以使用 = 运算符将值赋给变量。

> x = 7
> print(x)
7
= 运算符将数字 7 赋给变量 x。我们再次使用 print() 函数来输出 x 的值。现在我们可以使用 x 中的值进行其他计算。
> x = x * 9
> print(x)
63
> print(x*2) -- will not change the value of x
126
> print(x)
63

关于 Lua 数字类型的更多信息,您可以查阅 NumbersTutorial

字符串 (Strings)

Lua 也使用字符串(即文本)类型。要创建字符串,请将文本括在 "双引号"'单引号' 中。

> print("hello")
hello
我们可以像数字一样将字符串赋给变量。
> who = "Lua user"
> print(who)
Lua user
我们可以使用 .. 运算符将字符串连接(合并)在一起。
> print("hello ")
hello 
> print("hello " .. who) -- the variable "who" was assigned above
hello Lua user
> print(who)
Lua user

与其他一些语言不同,您不能使用 + 运算符来连接字符串。例如:

> message = "hello " + who
stdin:1: attempt to perform arithmetic on a string value
stack traceback:
        stdin:1: in main chunk
        [C]: ?

关于 Lua 字符串类型的更多信息,您可以查阅 StringsTutorial

布尔 (Boolean)

布尔值要么是 true,要么是 false。如果一个值不是 true,那么它一定是 false,反之亦然。not 运算符可以放在布尔值前面来反转它。例如,not true 等于 false

> x = true
> print(x)
true
> print(not x)
false
> print(not false)
true
布尔值用于表示逻辑测试的结果。等于 == 和不等于 ~= 运算符将根据提供给它们的值返回布尔值。
> print(1 == 0) -- test whether two numbers are equal
false
> print(1 == 1)
true
> print(1 ~= 0) -- test whether two numbers are not equal
true
> print(true ~= false) -- is true not equal to false?
true

请注意,赋值使用单个等号 (=),而比较使用双等号 (==)。这两个运算符有不同的含义但看起来相似,写错其中一个是很常见的错误。

关于 Lua 布尔类型的更多信息,您可以查阅 ExpressionsTutorial

表 (Tables)

Lua 有一种通用的“聚合”数据类型,称为表。聚合数据类型用于存储包含其他对象的集合(如列表、集合、数组和关联数组),这些对象本身也可以是数字、字符串,甚至是其他聚合类型。Lua 的独特之处在于,它使用表(它们是关联数组)来表示所有其他聚合类型。

表使用一对花括号 {} 创建。让我们创建一个空表。

> x = {}
> print(x)
table: 0035C910
(如果您的表没有像上面示例中那样唯一的标识符,这是正常的。)

TablesTutorial 将在稍后解释如何使用表。

函数 (Functions)

在 Lua 中,函数就像数字和字符串一样被赋给变量。函数使用 function 关键字创建。这里我们创建一个简单的函数,它会打印一条友好的消息。

> foo = function () print("hello") end -- declare the function
> foo() -- call the function
hello
> print(foo) -- get the value of the variable "foo"
function: 0035D6E8
请注意,我们可以打印变量 foo 的值,它会显示(与表类似)该值是一个函数,并且有一个表示该特定函数的唯一标识符。因此,作为与其他值相同的普通值,我们应该能够像其他值一样将函数赋给变量,我们也可以做到。
> x = function() print("hello") end
> x()
hello
> print(x)
function: 0035EA20
能够做到这一点是因为 Lua 具有“*头等值*”。这意味着所有值都以相同的方式对待。这是 Lua 一个非常强大且有用的特性。

函数可以成为表的一部分。

> a = "aeiou" -- a string
> b = 13      -- a number
> c = function()  -- a function
>  print ("\n\n\tAin't it grand")
> end
> d = { a, b ,c} -- put them in a table
> function printit(tata)  -- print their types.
>> for key, value in ipairs(tata) do print(key, type(value)) end
>> end
> printit(d)
1       string
2       number
3       function
        

FunctionsTutorial 将在稍后解释如何使用函数。

nil 值 (nil values)

nil 是一个特殊值,表示没有有用的值。如果您尝试获取一个不存在的变量,您将得到 nil。

> print(x)
nil
> x = 2.5
> print(x)
2.5

nil 在其他地方的使用将在其他教程中展示。

用户数据 (Userdata)

用户数据(Userdata)值是 Lua 之外的对象,例如用 C 实现的对象。当 C 库中的一个对象暴露给 Lua 时,通常会产生这些。您无法在 Lua 中对用户数据值做任何事情,除了传递它,它只用于传递给同一个 C 库暴露给的用户数据函数。但是,通过使用元方法(将在后续教程中解释),可以使用户数据与运算符一起工作,并表现得类似表。用户数据是一个更高级的主题,在 [Lua 参考手册] 中有更详细的讨论。

线程 (Thread)

线程(Thread)值代表一个独立的(协作式)执行线程。这些将在 CoroutinesTutorial 中进一步讨论。

动态类型 (Dynamic typing)

您可能已经注意到,在我们创建上述变量时,我们没有指定要创建哪种类型的变量。例如:

a = 1
b = "hello"
c = {}
在 C 等其他语言中,我们在创建变量时必须指定其类型。在 Lua 中,我们也可以将不同类型的值赋给同一个变量,例如:
a = 1
a = "hello"
a = {}
Lua 具有“*动态类型*”。这意味着类型是在程序运行时进行检查的。与许多其他具有动态类型的编程语言一样,在 Lua 中您不必指定变量的类型。变量通过赋给它的值或对象来知道自己的类型。

查询类型 (Querying type)

由于 Lua 是一种“*反射性*”语言,我们可以使用 Lua 函数 type() 来获取特定对象类型的描述。

> x = "123"  -- a string
> print(x, type(x)) -- show the value of x and its type
123     string
> x = x + 7  -- add a number to the string which forces coercion
> print(x, type(x)) -- again show the value and type
130     number

RecentChanges · preferences
编辑 · 历史
最后编辑于 2024 年 1 月 24 日下午 6:51 GMT (差异)