<h2 id="注释"><a href="https://Jaxes.github.io/#%E6%B3%A8%E9%87%8A" class="headerlink" title="注释"></a>注释</h2><figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 等号数量只要两边一致即可</span></span><br><span class="line"><span class="comment">--[=[多行注释]=]</span></span><br></pre></td></tr></table></figure>
标识符 A-Z、a-z、下划线_开头后加上0个或多个字母、下划线、数字(0-9)
注意 :最好不要使用下划线加大写字母,Lua保留字也是如此
不允许特殊字符@、$、%定义标识符
区分大小写
关键词 1 2 3 4 and break do else elseif end false for function local nil not or repeat return then true until while _VERSION
全局变量 1 2 3 4 5 6 7 8 9 print (b) b=10 print (b) b=nil print (b)
数据类型
nil(空) 1 2 3 4 5 6 7 8 9 10 11 12 print (type (a)) tab1={key1="val1" ,key2="val2" ,"val3" } for k,v in pairs (tab1) do print (k .. " - " .. v) end tab1.key1=nil for k,v in pairs (tab1) do print (k .. " - " .. v) end
boolean(布尔) 1 2 3 4 5 6 7 if false or nil then print ("至少有一个是true" ) else print ("false和nil都为false!" ) end
number(数字) 1 2 3 print (type (2 )) print (type (2e+1 ))
string(字符串) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 string1="this is string1" string2="this is string2" string3=[[ emmmmmmmmmm aaaaaaaaaaa ruaruaruaruarua! ]] print ("2" +6 ) print ("-2e2" *"6" ) print ("2" .."b" ) print (233 .. 666 ) len ="www.rua.com" print (#len ) print (#"helloworld" )
table(表) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 local tbl1={}local tbl2={"a" ,"b" ,"c" ,"d" }a["key" ]="value" a[1 ]=22 key=10 a[key]=22 a[a[key]]=a[key]+11 local tb1={"a" ,"b" ,"c" }for key,val in pairs (tb1) do print ("Key" ,key) end
function(函数) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 function fun1 (n) if n == 0 then return 1 else return n*fun1(n-1 ) end end print (fun1(5 )) fun2=fun1 print (fun2(5 )) function testFun (tab,fun) for k,v in pairs (tab) do print (fun(k,v)) end end tab={kay1="val1" ,key2="val2" } testFun(tab, function (key,val) return key .. "=" .. val end )
thread(线程) Lua中主要的线程是协程(coroutine)
线程可以同时多个运行,协程任意时刻只能运行一个
userdata(自定义类型) 用户自定义数据,用于表示一种由应用进程或C/C++语言库所创建的类型,可以将任意C/C++的任意数据类型的数据(通常是struct和指针)存储到Lua变量中调用