9.表的结构 —字典
a = {[“name”] = “唐老”,[“age”] = 14,[“1”] = 5}
print(a[“name”])
print(a[“1”])
print(a.name)
–新增
a[“sex”] = “b”
print(a.sex)
–删除
a[“sex”] = nil
for x,y in pairs(a) do
print(x,y)
end
Student = {
age = 1,
sex = true,
Up = function()
print(student.age)
print(“我成长了”)
end,
Lear = function(t)
print(t.sex)
print("天天学习")
end
}
print(Student.age)
10.表中和表外的声明
–表外声明
Student.name = “123”
Student.Speak = function()
print(“表外声明”)
end
–表外声明2
function Student.Speak2()
print(“表外声明2”)
end
–在表内调用属性,也要student.属性
Student.Learn(Student)
–冒号会把自身作为第一个参数写入
Student.Learn(Student)
Student:Learn()
11.多脚本执行
1.local 局部的修饰词
2.function中的全局变量,若无法调用function,则变量也未声明
3.require(“脚本文件名”) 后可以调用该名脚本中的变量,函数,并在首次执行其中的代码
4.package.loaded[“脚本名”] 执行过为true,否则为nil
5.在_G的表中存储着所有声明的全局变量,包括迭代器和函数,但是局部变量不行。
12.lua的特殊语法
1.多返回值时,多返回值时忽略,多接收值就补空,多变量赋值应相同
2.在lua中,只有nil和false被认为是假,常量0 不是假。
3.三目运算符,and前假则假,or前真则真
13.lua中的协程
co1 = coroutine.create(fun) 需要function的参数,声明协程
co2 = coroutine.wrap(fun) 该函数的类型为function,声明协程
coroutine.resume(co1) 协程的启动
co2() 协程的启动
协程挂起
coroution.yeld(i) 挂起协程,返回值为treu和i的值
协程状态
dead 结束
suspended 暂停
running 进行中
coroutine.status(co)
显示正在运行的协程号
print(coroutine.running())