lua的基本类型
数字、布尔类型
lua不分整数和小数
表
函数
字符串
数字、布尔类型
lua不分整数和小数
nil等同于null
-- 基本类型 print("基本类型测试-----------------------------------------------------") print(5.2) print(5) print(math.floor(5.2)) print(true) print(nil) -- 空,相当于null
输出
基本类型测试-----------------------------------------------------
5.2 5 5 true nil
变量
除了基本类型,其他放的都是引用
作用域
local 局部变量
前面不加 则为全局变量
代码
-- 变量 print("局部变量测试-----------------------------------------------------") tat = "hello tat" local tmp = "hell o"-- 存放字符串的引用 print(tat) print(tmp) local obj = nil print(tmp) x,y = 1,2 --多变量赋值 print(x,y) x,y = y,x --赋值时会先计算等号后边的值,相当于交换 print(x,y) a, b, c = 0, 1 --> 0 1 nil --a. 变量个数 > 值的个数 按变量个数补足nil print(a,b,c) a, b = 2,3,4 --b. 变量个数 < 值的个数 多余的值会被忽略 print(a,b,c)
输出
局部变量测试----------------------------------------------------- hello tat hell o hell o 1 2 2 1 0 1 nil 2 3 nil4.复杂对象
表
函数
字符串
表,类似一个键值对的Map
--表 print("表测试-----------------------------------------------------") local list={ [0] = 1, --整数索引 test_key = 3,--字符串索引 ["0"] = 4, --字符串索引,和数字0索引不一样 } print(list)--打印出的是地址 print(list[0]) print(list["0"]) print(list["test_key"]) print(list.test_key) list.test_key = "aasdas" print(list.test_key) local list2 = {"apple", "pear", "orange", "grape"}-- 等同于数组,下标从1开始的键值对 for k, v in pairs(list2) do print(k .. " : " .. v) end
输出
表测试----------------------------------------------------- table: 0D651730 1 4 3 3 aasdas 1 : apple 2 : pear 3 : orange 4 : grape
函数
--函数 print("函数测试-----------------------------------------------------") function add_func(parm1,parm2) print("function insert") print(parm1) print(parm2) print("function end") end add_func(3,4) local func = add_func func(8,9)
输出
函数测试----------------------------------------------------- function insert 3 4 function end function insert 8 9 function end
字符串
print("字符串测试-----------------------------------------------------") local str = "a" local str2 = "b" local str3 = [[ 这个是多行字符串测试 哈哈 ]] print(str) print(str3) --print(str+str2)--字符串相加错误写法 print(str..str2)--字符串相加正确写法
输出
字符串测试----------------------------------------------------- a 这个是多行字符串测试 哈哈 ab
本来是看视频记录来着,后来看到http://www.runoob.com/lua/lua-loops.html 菜鸟教程上面挺详细及全面的介绍了,感觉看那个就好了。
Lua学习之一环境搭建:https://blog.csdn.net/cmqwan/article/details/80742135
Lua学习之二基本类型:https://blog.csdn.net/cmqwan/article/details/80742990
Lua学习之三流程控制:https://blog.csdn.net/cmqwan/article/details/80749169
Lua学习之四循环 :https://blog.csdn.net/cmqwan/article/details/80749241
Lua学习之五面向对象:https://blog.csdn.net/cmqwan/article/details/80749348
Lua学习之六模块 :https://blog.csdn.net/cmqwan/article/details/80752806
Lua学习之七源码 :https://blog.csdn.net/cmqwan/article/details/80752873