类型
nil 空 boolean 只有false和nil是否,其他都是true number对应c的double,新版有64位的整形 tonumber("100");转为number string tostring(100); [[]]多行字符串赋值[[<html></html>]] ..拼接字符串 string.len(str)获取长度 string.sub(str,3,5)截取子字符串,下标从1开始 string.find(str,"num")查找,返回下标 string.gsub(str,"nuu","num")替换所有符合的内容
table
--map > a = {} > a ={x=1,y=12} > print(a.x) 1 --添加 > a.z=33 > print(a.z) 33
--数组 > tb={100,200,300} > print(tb[1]) 100 --末尾添加 > table.insert(tb,400) > print(tb[4]) 400 --在索引5处添加 > table.insert(tb,5,500) --移除最后一项 > table.remove(tb) 400 > print(tb[4]) nil --遍历 for k,v in ipairs(tb) do print(k,v) end 1 100 2 200 3 300 Table={name="airjronda",age=13} print(Table["name"])--要带引号
元表
index 如果是表,则在本表中查找key时,本表中没有此key,到index的表里找
如果是方法,则执行此方法,table和key可做参数
t1={id=123} meta1={__index={age=12}} setmetatable(t1,meta1) print(t1.age)--12 meta2={__index=function(table,key) print(table,key) end} setmetatable(t1,meta2) print(t1.age) --table地址 age --nil
newindex 如果是表,给本表里设一个没有的key时,直接在newindex这个表里添加
t1={id=123} t2={} meta1={__newindex=t2} setmetatable(t1,meta1) t1.age=12 print(t1.age,t2.age)--nil 12
如果是方法,给本表里设一个没有的key时,会直接调用这个方法,新增的key,value可以作为参数
t1={id=123} t2={} meta2={__newindex=function(table,key,value) print(tostring(table).." add "..key..":"..value) rawset(table,key,value)--在表里添加 end} setmetatable(t1,meta2) t1.age=12 print(t1.age,t2.age) --table: 01278AC8 add age:12 --12 nil
面向对象
self
t1={id=123} function t1:getId()
--谁调用谁就是self return self.id; end print(t1:getId())
继承
father={} function father:faSay() print("faSay") end --自索引,自己作为自己的index father.__index=father son={} function son:sonSay() print("sonSay") end --father设置为son的元表 setmetatable(son,father) son.sonSay() son.faSay()
new
userinfo={id="123",name="tom"} userinfo.__index=userinfo function userinfo:new(obj) obj=obj or {} setmetatable(obj,self) return obj end function userinfo:addAge(val) self.age=self.age+val;-- 如果没有age,先到userinfo里取age加法后设置给user2 end user2=userinfo:new({id="323"}) print(user2.id)--323 print(user2.name)--tom user2.name="jerry" print(user2.name)--jerry
多重继承
userinfo={id="123",name="tom"} function userinfo:new(obj) obj=obj or {} setmetatable(obj,self) self.__index=self return obj end
私有化
function newUser() local member={ id="12", age=12, name="adf" } local function getId() return member.id; end local function getAge() return member.age; end return { getId=getId, getAge=getAge, getName=function() return member.name; end }; end users=newUser(); print(users.getId(),users.getAge(),users.getName())
协程
function getMoney(a,b) x,y,z=coroutine.yield(a*2,b*2,"100")--在这里挂起 print(x,y,z) return x,y,z; end handle=coroutine.create(getMoney) print(coroutine.status(handle)) print(coroutine.resume(handle,12,34))--阻塞,执行到yield,返回true,24,68,100 print(coroutine.status(handle))--挂起状态 print(coroutine.resume(handle,"a","b","c"))--将值传给x,y,z,返回true a b c print(coroutine.status(handle))--dead
交替执行
function fun1() while true do coroutine.resume(handle2) print("fun1---1") end end function fun2() while true do print("xxxxxxxxx") coroutine.yield();--第一次到这返回,第二次从这后面继续 print("fun2---2") end end handle1=coroutine.create(fun1) handle2=coroutine.create(fun2) coroutine.resume(handle1)
生产消费
function Product() local i=0; while true do i=i+1; coroutine.yield(i);--将i返回 end end function Cast() while true do local res,n=coroutine.resume(handle1); print(res,n);--true i end end handle1=coroutine.create(Product) handle2=coroutine.create(Cast) coroutine.resume(handle2)