在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
执行下面的脚本用luajit test.lua即可 一、变量及逻辑运算 --number, string, boolean, table, function, thread, userdata, nil --<1>Number demo x = 11 --It's number type,but not an integer --The two common variable name below in the lua language is recommended. exampleVar = 10e5 --equal 1000000 example_var = 666 exampleHexadecimal = 0xFAE --equal 4014 --<2>String demo exampleStrings = "Hello 123 &^*& \n" --nil print(notDefineVariableIsNil) -- nil -- MathOperator demo -- (),a ^b, not a, #a, -a, a*b, a/b, a%b, a+b, a-b, a..b, a<b, a>b(return boolean value), a ~= b(tilde equals !), a == b -- a and b, a or b exampleMathModuloOperator = 10/3 --Supporting + - * / %, and so forth. exampleDecimalRemainder = 10 % 3.3 -- equal 0.1 exponentNum1 = 2 exponentPower = 4 exampleExponentCal = exponentNum1 ^ exponentPower -- 16 exampleBoolean = true print(not exampleBoolean) -- false print(-exponentNum1) -- -2 print( (exponentNum1 + exponentPower) * 4) -- 24 --逻辑运算符 exponentNum1 = 10 exponentPower = 4 print(exponentNum1 and exponentPower) -- 4,因为第一个参数为true,所以返回4 print(exponentNum1 or exponentPower) --10,因为第一个参数为true exponentNum1 = false print(exponentNum1 and exponentPower) --false,因为第一个参数为false,所以返回false a = 4 b = 10 print( a < b or "this statement is false") --true,or的话第一个参数为true直接返回 print( a > b or "this statement is false") --this statement is false,or的话第一个参数为false返回第二个参数 print( not(a + 11 < b) or "this statement is false") --true,or的话第一个参数为true直接返回 -- Catenation string exampleCatenationString = "Hello " .. "World!" -- Hello World! print(#exampleCatenationString) -- 12 --print(exampleCatenationString) 二、条件语句 --条件运算 if 3 > 5 then print("this statement is true") elseif 20 > 15 then print("20 > 15") else print("this statement is false") end --常用函数 print(type(20)) --number,类型为number类型 testVar = nil if type(testVar) == "number" then print("testVar is a number") elseif type(testVar) == "string" then print("testVar is a string") else print(testVar) print("testVar is a else") end 三、循环
print("------while loop example------") iter = 0 while iter <= 10 do print(iter) iter = iter + 1 end print("------for loop example------") --等同于while循环,默认递增为1,所以不用写 for i = 0, 10 do print(i) end print("-----改变递增系数-----") --以3为递增 for i = 0, 10, 3 do print(i) end print("-----增加退出循环条件-----") for i = 0, 10 do print(i) if i == 5 then break end --假如比较简单,可以直接一行 end print("------until loop example------") element = 0 repeat print(element) element = element + 1 until element > 10 四、基本表 t = {1, "hello", true, four = 4, five = true, six = "world"} --可以放置任何类型 print(t[2]) --hello print(t[4]) --nil,直接打印第4个元素为nil --以键值对的方式访问 print(t["four"]) --4 print(t.four) --4 print(t.five, t["six"]) --true world --sizeof表 print(#t) --3,nil的不算在内 examT = {1, 2, 3, 4, 5, 6} print(#examT) --6 examString = "know" print(#examString) --4 五、函数 (1)命名函数的两种方式. function f() print("Hello") end g = function() print("Hello again") end f() --推荐这种用法 g() function println(value) print(value) end println("Arun") println(10) y = 911 --全局变量 function addPrint(a, b) x = "101010101010" print(x) y = 912 return a + b end t = {addPrint(1, 2), 23} println(t[1]) print(x) print(y) (2)变量的作用域 --x = 12 --打开这个变量,可以看出x变量的值的范围 function add(a, b) x = 10 --本地变量 local y = 11 print(x) --10 return a + b end print(x) --nil print(add(5,15)) --20 print(x) --10,函数内部加local关键字,它会变成全局变量 print(y) --nil 六、内存管理 注意内存的传值引用和传址引用 ------------------ x = 10 y = x x =20 print(x) print(y) --[[输出=> 20 10]] ------------------ m = {10, 20, 30} n = m m[2] = 40 print(m[2]) print(n[2]) --[[输出=> 40 40]] 七、闭包 function f() local x = 1 return function() print(x); end end printHello = f() printHello() --1 ------------------------------- function createIter() local i = 0 return function() print(i); i = i + 1 end end iter = createIter() iter() --0 iter() --1 iter() --1 八、table面向对象实现 Player = { x = 0, y = 0, name = "", new = function() p = {} for k, v in pairs(Player) do p[k] = v end return p end, move = function(obj, x,y) obj.x = obj.x +x obj.y = obj.y +y end } p1 = Player.new() p1.x = 10 p1.y = 20 p1.name = "Bob" p2 = Player.new() p2.x = 30 p2.y = 50 p2.name = "Steve" print(p1.x, p1.y, p1.name) --10 20 Bob print(p2.x, p2.y, p2.name) --30 50 Steve p1.move(p1, 10, 10) p2.move(p2,70,90) print(p1.x, p1.y) --20 30 print( p2.x, p2.y) --100 140 九、meta表高级用法,重写原始操作符 Vector2 = { x = 0, y =0, mt = {}, New = function() local vec = {} setmetatable(vec, Vector2.mt) vec.x = Vector2.x vec.y = Vector2.y vec.mt = Vector2.mt vec.Translate = Vector2.Translate return vec end, Translate = function(self, dx, dy) self.x = self.x + dx self.y = self.y + dy end } Vector2.mt.__add = function(v1, v2) local vec = Vector2.New() vec.x = v1.x + v2.x vec.y = v1.y + v2.y return vec end Vector2.mt.__sub = function(v1, v2) local vec = Vector2.New() vec.x = v1.x - v2.x vec.y = v1.y - v2.y return vec end -- __mul, __div, __mod, Vector2.mt.__eq = function(v1, v2) return v1.x == v2.x and v1.y == v2.y end -- __lt, __le, __gt, __ge Vector2.mt.__tostring = function(vec) return "(" .. vec.x .. ", " .. vec.y .. ")" end Vector2.mt.__metatable = "Private" --[[Vector2.mt.__index = Vector2 Vector2.mt.__newindex = function(t, k, v) error("Cannot change values of Vector2 instance.") end]] v1 = Vector2.New() v1.x = 10 v1.y = 20 --v1.Translate(v1, 10, 10) --等同于 --v1:Translate(10, 10) --print(v1.x, v1.y) --20 30 v2 = Vector2.New() v2.x = 30 v2.y = 40 v3 = v1 + v2 print(v3.x, v3.y) --40 60 v4 = v1 - v2 print(v4.x, v4.y) -- -20 -20 print(v4) -- (-20, -20) print(getmetatable(v4)) -- Private print(v1 == v2) -- false --setmetatable(v4, nil) --test.lua:71: cannot change a protected metatable 十、高级循环 t1 = {1, 2, 3} t1[2] = nil for i=1, #t1 do print(i, t1[i]) end --[[1 1 2 2 3 3]] t2 = {one = 1, two = 2, three = 3} for k,v in pairs(t2) do --是pairs而不是ipairs print(k, t2[k]) end --[[ one 1 three 3 two 2]] t3 = {1, 2, 3, 4, 5} function numIter(tb, start) i = start return function() i = i + 1 if tb[i - 1] then return i - 1, tb[i - 1] else return nil end end end for k,v in numIter(t3, 1) do print(k, v) end --[[ 1 1 2 2 3 3 4 4 5 5]] 十一、lua内运行外部代码的三种方式 (1)test1.lua for i = 0, 10 do print("hello") end return 100 (2)main.lua -- dofile, loadfile load --<1>加载执行文件第一种方式 --dofile("/Users/00arunalldata00/006_eleallproject/002ngconf/002_camel-agent-deploy/router-lua-module/aruntest/lua_learning/test1.lua") --<2>加载执行文件第二种方式 --[[function newDoFile(filename) f = assert(loadfile(filename)) return f() end newDoFile("./test1.lua") print(newDoFile("./test1.lua")) -- 100]] --<3>加载执行文件第三种方式 --(1)load和函数调用第一种区别 f = load("print(20)") f() -- 20 function g() print(20) end g() -- 20 --(2)load和函数调用第二种区别 x = 10 local x = 20 function f1() x = x + 1 print(x) end f1() -- 21 g1 = load("x = x + 1;print(x)") g1() -- 11 十二、
Reference: https://www.youtube.com/playlist?list=PL0o3fqwR2CsWg_ockSMN6FActmMOJ70t_ |
请发表评论