Lua 学习笔记(一)

时间:2023-03-08 16:04:10

Lua学习笔记

1、lua的优势

a、可扩张性     b、简单     c、高效率     d、和平台无关

2、注释

a、单行注释 --        b、多行注释 --[[  --]]

3、类型和值  

8个基本类型,检测变量类型用type
  a、nil         print(type(nil))  -->nil    
     全局变量没有赋值以前,会被默认nil类型

b、boolean     print(type(true)) -->boolean
       除了false和nil为假,其余都为真,所以lua中,0和空字符串都是真的

c、number
     表示实数

d、string
       1)  字符串不可修改
       2)  "\" 转义
       3)  "[[ ... ]]" 符号表示字符串,可以跨行
       4)  ".." 是字符串连接符, 数字之间 需要空格 避免错误解释
       5)  字符串和数字之间可以自动装换 ,但是 10 == "10" 比较永远是false, 需要使用tonumber() 将字符串转换为数字,反之用tostring()

e、userdata

f、function
       函数可以做参数,也可以做返回值

g、thread

h、table

4、表达式

a、算术运算符
       使用在number之间,+ - * /  包括负数符号 "-"

b、关系运算符
     <  >  <=  >=  == ~=
     比较的时候要保证比较值之间的类型一致

c、逻辑运算符
     and  or  not
     c = a and b   --a为false,a赋值给c,否则,将b赋值给c
     c = a or  b   --a为true, a赋值给c,否则,将b赋值给c
     not 返回true 或 false,类似 !

and优先级高于 or

d、连接符
     ..

e、运算符优先级

f、表的构造 {}

 5、基本语法

a、 赋值语句

b、局部变量和代码块
       使用local 创建一个局部变量
       局部变量在 do..end内

c、控制结构语句  

 if condition then
then-part
end; if condition then
then-part
else
esle-part
end; if condition then
then-part
elseif conditions then
elseif-part
...
else
else-part
end;

d、循环语句

 while condition do
do-part
end; repeat
repeat-part
until condition; for var = exp1,exp2,exp3 do
do-part
if condition then
break;
end;
end; for i,v in inpairs() do
statment;
end;

6、函数

a、 语法
       function func_name ( argument-list )
            statment-list
       end;

b、返回多个值

c、可变参数

d、命名参数