035_lua快速入门
执行下面的脚本用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_
035_lua快速入门的更多相关文章
- Web Api 入门实战 (快速入门+工具使用+不依赖IIS)
平台之大势何人能挡? 带着你的Net飞奔吧!:http://www.cnblogs.com/dunitian/p/4822808.html 屁话我也就不多说了,什么简介的也省了,直接简单概括+demo ...
- SignalR快速入门 ~ 仿QQ即时聊天,消息推送,单聊,群聊,多群公聊(基础=》提升)
SignalR快速入门 ~ 仿QQ即时聊天,消息推送,单聊,群聊,多群公聊(基础=>提升,5个Demo贯彻全篇,感兴趣的玩才是真的学) 官方demo:http://www.asp.net/si ...
- 前端开发小白必学技能—非关系数据库又像关系数据库的MongoDB快速入门命令(2)
今天给大家道个歉,没有及时更新MongoDB快速入门的下篇,最近有点小忙,在此向博友们致歉.下面我将简单地说一下mongdb的一些基本命令以及我们日常开发过程中的一些问题.mongodb可以为我们提供 ...
- 【第三篇】ASP.NET MVC快速入门之安全策略(MVC5+EF6)
目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...
- 【番外篇】ASP.NET MVC快速入门之免费jQuery控件库(MVC5+EF6)
目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...
- Mybatis框架 的快速入门
MyBatis 简介 什么是 MyBatis? MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架.MyBatis 消除 了几乎所有的 JDBC 代码和参数的手工设置以及结果 ...
- grunt快速入门
快速入门 Grunt和 Grunt 插件是通过 npm 安装并管理的,npm是 Node.js 的包管理器. Grunt 0.4.x 必须配合Node.js >= 0.8.0版本使用.:奇数版本 ...
- 【第一篇】ASP.NET MVC快速入门之数据库操作(MVC5+EF6)
目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...
- 【第四篇】ASP.NET MVC快速入门之完整示例(MVC5+EF6)
目录 [第一篇]ASP.NET MVC快速入门之数据库操作(MVC5+EF6) [第二篇]ASP.NET MVC快速入门之数据注解(MVC5+EF6) [第三篇]ASP.NET MVC快速入门之安全策 ...
随机推荐
- Hadoop — Yarn原理解析
1. 概述 Yarn是一个资源调度平台,负责为运算程序提供服务器运算资源,相当于一个分布式的操作系统平台:而MapReduce等运算程序则相当运行于操作系统之上的应用程序. 2. YARN的重要概念 ...
- java 写一个 map reduce 矩阵相乘的案例
1.写一个工具类用来生成 map reduce 实验 所需 input 文件 下面两个是原始文件 matrix1.txt 1 2 -2 0 3 3 4 -3 -2 0 2 3 5 3 -1 2 -4 ...
- IO流--字符流与字节流--File类常用功能
IO流的常用方法: 1: 文件的读取和写入图解: 2:字节流: 读写文件的方法: 一般效率读取: 读取文件: FileInputStream(); 写数据: Fil ...
- 如何更改vs2013中git的远程仓库url地址
可以通过修改Git库配置文件实现,请看下图:
- Elasticsearch入门实践
官网:https://www.elastic.co/ 下载:https://www.elastic.co/downloads/elasticsearch 文档:https://www.elastic. ...
- spring boot零碎知识点待补充
@Controller 和@RestController的区别 @RestController相当于同时使用了@Controller和@ResponseBody 即不会使用视图解析器,返回值直接返回 ...
- 华为平板安装APK,提示“该安装包未包含任何证书”
有的平板上会有错误现象 打包时签名勾选v1即可.
- vivalidi 一款由Web技术诞生的Web浏览器
vivalidi https://vivaldi.com/ A million ways to customize everything The world is a colorful place b ...
- Javaweb学习笔记——(十)——————response对象,response字符流缓冲器,响应头,状态码,重定向,requset对象,路径和乱码
请求响应对象: request和response *当服务器接收都请求后,服务器会创建request和response对象,把请求数据封装到request对象中: *然后调用Servlet的sevic ...
- Creating A Moddable Unity Game
前言: 对游戏进行修改与拓展(MOD)是我一直以来感兴趣的东西,我的程序生涯,也是因为在初中接触到GBA口袋妖怪改版开始的,改过也研究过一些游戏的MOD实现方式,早就想在自己的游戏中实现“MOD系统” ...