-- mac 上有很多文本编辑器,Himi找了很多,感觉TextMate是比较好用的,小巧且功能强大!而且免费,图标是个小菊花 - -
-- 注释语句
--[[
注释段落语句
]]
--
--引用其他lua文件,不需要加上(.lua)后缀
--require "xx"
--变量不需要定义,可以直接赋值
count
=
100
--成员变量
local
count
=
100
--局部变量
--方法定义
function hello
(
...
)
--打印
print
(
"Hello Lua!"
)
;
print
(
string
.format
(
...
)
)
end
-- 每一行代码不需要使用分隔符,当然也可以加上
-- 访问没有初始化的变量,lua默认返回nil
-- 调用函数形式
hello
(
"你懂的"
)
--打印变量的类型
isOK
=
false
print
(
type
(
isOK
)
)
-- 基本变量类型
a
=
nil
--Lua 中值为nil 相当于删除
b
=
10
c
=
10.4
d
=
false
--定义字符串,单引号,双引号都可以的
e
=
"i am"
d
=
'himi'
--两个字符串的连接可以如下形式
stringA
=
"Hi"
stringB
=
"mi"
print
(
stringA..stringB
)
--另外Lua也支持转移字符,如下
print
(
stringA..
"\n"
..stringB
)
;
--修改字符串的部分gsub,可以如下形式:(将stringA字符串中的Hi修改为WT)
stringA
=
string
.gsub
(
stringA
,
"Hi"
,
"WT"
)
print
(
stringA
)
;
--将字符换成数字tonumber(不转也会自动转)
--将数字换成字符tostring(不转也会自动转)
stringC
=
"100"
stringC
=
tonumber
(
stringC
)
stringC
=
stringC
+
20
stringC
=
tostring
(
stringC
)
print
(
stringC
)
--取一个字符串的长度使用 #
print
(
#stringC)
--创建 表
tableA
=
{
}
m
=
"x"
tableA[m]
=
100
m
2
=
'y'
tableA[m
2
]
=
200
print
(
tableA[
"x"
]..
"\n"
..tableA.y
)
--另外表还可以如下形式(从1开始)
tableB
=
{
"4"
,
"5"
,
"6"
,
"7"
,
"8"
}
print
(
tableB[
1
]
)
--算术操作符
c
1
=
10
+
2
c
2
=
10
-2
c
3
=
10
*
2
c
4
=
10
/
2
c
5
=
10
^
2
c
6
=
10
%
2
c
7
=
-10
+
2
print
(
c
1.
.
"_"
..c
2.
.
"_"
..c
3.
.
"_"
..c
4.
.
"_"
..c
5.
.
"_"
..c
6.
.
"_"
..c
7
)
--控制操作
--if then elseif then else end
abc
=
10
if
abc
=
=
10
then
print
(
"v1"
)
elseif abc
=
=
9
then
print
(
"v2"
)
else
print
(
"v3"
)
end
--for
--从4(第一个参数)涨到10(第二个参数),每次增长以2(第三个参数)为单位
for
i
=
4
,
10
,
2
do
print
(
"for1:"
..i
+
1
)
end
--也可以不制定最后一个参数,默认1的增长速度
for
i
=
4
,
10
do
print
(
"for2:"
..i
+
1
)
end
tableFor
=
{
"himi1"
,
"himi2"
,
"himi3"
,
"himi4"
,
"himi5"
}
for
k
,
v
in
pairs
(
tableFor
)
do
print
(
"for3:key:"
..k..
"value:"
..v
)
end
--while
w
1
=
20
while
true
do
w
1
=
w
1
+
1
if
w
1
=
=
25
then
break
end
end
print
(
"whlile:"
..w
1
)
--repeat
aa
=
20
repeat
aa
=
aa
+
2
print
(
"repeat:"
..aa
)
until aa
>
28
--关系操作符
--需要注意的是不等于符号 ~= 而不是!=
ax
=
10
bx
=
20
if
ax
>
bx
then
print
(
"GX1"
)
elseif ax
<
bx
then
print
(
"GX2"
)
elseif ax
>
=
bx
then
print
(
"GX3"
)
elseif ax
<
=
bx
then
print
(
"GX4"
)
elseif ax
=
=
bx
then
print
(
"GX5"
)
elseif ax~
=
bx
then
print
(
"GX6"
)
else
print
(
"GX7"
)
end
关于函数
--函数详解
--有一个返回值的函数
function funTestBackOne
(
aCount
)
local
aCount
=
aCount
+
1
return
aCount
end
a
=
20
print
(
funTestBackOne
(
a
)
)
--有多个返回值的函数
function funTestBackMore
(
)
return
2
,
3
end
a
,
b
=
funTestBackMore
(
)
print
(
a..
" and "
..b
)
--有变长参数的函数
function funTestUnKnow
(
...
)
print
(
...
)
end
funTestUnKnow
(
a
,
b
,
"Himi"
)
--闭合函数(一个函数写在另外一个函数内)
function funTest
1
(
...
)
local
d
=
12
;
d
=
d
+
...
function funTest
2
(
...
)
print
(
d
)
end
funTest
2
(
)
end
funTest
1
(
100
)
day1
--[[function fuc(n)
if n==0 then
return 1
else
return n*fuc(n-1)
end
endprint("input a number")
a = io.read("*number")
print(fuc(a))
--]]--替换字符串
--[[
a = "one thing"
b = string.gsub(a,"one","two")
print(a)
print(b)
--]]--转换数字
--[[
line = io.read()
n = tonumber(line)
if n==nil then
error(line.."is not a valid number")
else
print(n*2)
end
--]]
--拼接字符串
--[[
print("10"+1)print("hell".."o")
--]]--转换
--[[
line = io.read()
n = tonumber(line)
print(n)print("我是一个字符串"..tostring(n))
print("10"=="10")
print(tostring(10)=="10")
--]]--#计算长度
--[[
a = "hello"
print(#a)
print(#"good\0byte")
]]----table的使用
--[[
a = {}
k = "x"
a[k] = 10
a[20] = "great"
print(a[k])
print(a[20])
k = 20
print(a[k])
a["x"] =a["x"]+1
print(a["x"])--使用同一个table
a = {}
a["x"] = 10
b = a --a和b使用了同一个table
print(b["x"])
b["x"] = 20
print(a["x"])
a = nil --b还在用table
b = nil --没人用table了--自动增长
a = {}
--创建1000个新条目
for i = 1,1000 do a[i] = i*2 end
print(a[9])
a["x"] = 10
print(a["x"])
print(a["y"])
a.x = 20
print(a["x"])
print(a.x)--循环存储值
a = {}
for i = 1,10
do a[i] = io.read() end--输入打印数组的值
for i = 1,#a do
print(a[i])
end
--输出最后一个值
print(a[#a])
a[#a] = nil --删除最后一个值
a[#a + 1] = v --将v添加到列表的末尾
--]]--[[
a = {}
a[1000] = 10
a[10000] = 11
print(table.maxn(a))a = {}
i = 10;j = "10";k = "+10"
a[i] = "one value"
a[j] = "another value"
a[k] = "yet another value"
print(a[i])
print(a[j])
print(a[k])
print(a[tonumber(j)])
print(a[tonumber(k)])
--]]--取小数点后面几位
--[[
x = math.pi
print(x%0.1)
print(x - x%0.01)local tolerance = 10
function isturnback(angle)
angle = angle%360
return (math.abs(angle-180)<tolerance)
end
print(isturnback(-180))
--]]--[[
a = 10
b = "10"
if tostring(a)~=b then print("不等")
else
print("相等")
end
--]]--[[
a = {}
a.x = 1
a.y = 2
b = {}
b.x = 1
b.y = 2
c = a --a 等于 c table只有两个变量引用同一个的时候才是相等的
if a==b then print("a==b")
else
print("a~=b")
end
--]]--[[
days = {"Sunday","Monday","Tuesday","Wednessday","Thursday","Friday","Saturday"}
print(days[1]) --这里注意不是从0开始--a={x = 10,y = 20} <=>a = {};a.x = 10;a.y = 20
w = {x = 0,y = 0, label = "console"}
x = {math.sin(0),math.sin(1),math.sin(2)}
w[1] = "anther field"
x.f = w
print(w["x"])
print(w[1])
print(x.f[1])
w.x = nil
--]]--[[
list = nil
for line in io.lines() do
list = {next = list,value = line}
endlocal l = list
while l do
print(l.value)
l = l.next
end
--]]--error("inva operation")
--相当于do while
--[[
repeat
line = io.read()
until line ~=""
print(line)for i = 1,math.huge do --math.huge 无限大
print(1)
end
--]]
--[[
days = {"Sunday","Monday","Tuesday","Wednessday","Thursday","Friday","Saturday"}revDays = {["Sunday"] = 1,["Monday"] = 2,["Tuesday"] = 3,["Wednessday"] = 4,["Thursday"] = 5,["Friday"] = 6,["Saturday"] = 7}
x = "Tuesday"
--print(revDays[x])table.sort(revDays)
revDays1 = {}
for k,v in pairs(revDays) do --给v赋值“Sunday","Monday"... k赋值1,2...
print(k)
print(v)
end--数组排序
a = {1,3,2}
table.sort(a)
for i,v in pairs(a) do print(v) end--]]
--print("Monday是第"..revDays1["Monday"].."天")
--[[
for key,value in ipairs(revDays1) do
print(revDays1[key])
end
--]]--[[
a = {}
a[1] = 10
a[2] = 20
a[3] = 30
for i,v in pairs(a) do print(v) end
--]]
day2
--函数
--数组求和
--[[
function sum(a)
local sum = 0
for i,v in pairs(a) do
sum = sum + v
end
return sum
enda = {1,2,3,4,5,6}
num = sum(a)
print(num)s,e = string.find("hello lua","lua")
print(s,e) --返回函数的开始结束字符,从1开始--]]
--返回多个返回值
--[[
function maximum(a)
local mi = 1
local m = a[mi]
for i,val in pairs(a) do
if val> m then
mi = i;m = val
end
end
return m,mi
end
--]]--[[
function maxmum(a)
local num = 1
local val = a[num]
for i,value in pairs(a) do
if value>val then
num = i
val = value
end
end
return val,num
endprint(maxmum({8,10,23,12,5}))
--]]t = {}
s = "from=world, to=Lua"
for k, v in string.gmatch(s, "(%w+)=(%w+)") do
t[k]=v
end
for k, v in pairs(t) do
print(k,v)
end
print({if not package.loaded["111"] then print("nil") end}
day3
--面向对象
People = {} --定义一个p表
function clone(tab)
local t = {}
for key,value in pairs(tab) do
t[key] = value
end
return t
end--[[
function People.sayhi()
print("Hello World")
end--]]--[[People.sayhi = function()
print("Hello People ")
end--]]function People.new(name)
local self = clone(People)
self.name = name
return self
endPeople.sayhi = function(self)
print("Hello People "..self.name)
end--[[q = clone(People)
q.sayhi()--]]local k = People.new("丁小未")
k.sayhi(k)
k:sayhi(k)
--k.sayhi()
--继承
function copy(dist,tab)
for key,value in pairs(tab) do
dist[key] = value
end
endMan = {}
Man.new = function(name)
local self = People.new(name)
copy(self,Man)
return self
endMan.sayHello = function()
print("Man say hello")
endMan.sayhi = function(self)
print("hi "..self.name)
endlocal m= Man.new("人")
m.sayhi(m)
m:sayHello()
--m:sayhi(m)
lua不支持函数重载
--可变参数
function show(...)
local num = select('#',...)
if num>0 then
print("恩")
else
print("无")
end
endshow()
function clone(tab)
local t = {}
for key,value in pairs(tab) do
t[key] = value
end
return t
end--[[
function People.sayhi()
print("Hello World")
end--]]
function People.new(name)
local self = clone(People)
self.name = name
return self
endPeople.sayhi = function(self)
print("Hello People "..self.name)
end
local k = People.new("丁小未")
k.sayhi(k)
--原表
local tbA = {a = 1}
local tbB = {b = 2}
tbA.y = function(tb, k) --必须要有返回值
print("__index key:" .. k)
if k == 'a' then
return 10
end
return nil
end
--[[
local mt = {__index = tbA,
__newindex = function(t, k, v)
end
}local mt = {__index = function(tb, k) --这个元方法用于读取值
print("__index key:" .. k)
if k == 'a' then
return 10
end
return nil
end,
__newindex = function(tb, k, v) --这个是用于设置值
print("__newindex key:" .. k)
end
}
]]
setmetatable(tbB, tbA)
tbB.a = 3
print(tbB.a)
--tbB.d = 3
--tbB.b = 5
--print(tbB.d)
标签:function,10,end,..,lua,--,Lua,初识,print
From: https://blog.51cto.com/dingxiaowei/6254921