-- mac 上有很多文本编辑器,Himi找了很多,感觉TextMate是比较好用的,小巧且功能强大!而且免费,图标是个小菊花 - -
]]
--
=
100
--成员变量
--局部变量
(
...
)
--打印
"Hello Lua!"
)
;
.format
)
"你懂的"
)
false
isOK
)
nil
--Lua 中值为nil 相当于删除
10
10.4
false
"i am"
"Hi"
"mi"
)
"\n"
..stringB
;
.gsub
,
"Hi"
"WT"
)
"100"
stringC
)
20
)
{
}
"x"
100
2
'y'
]
200
"x"
]..
..tableA.y
)
"4"
"5"
"6"
"7"
"8"
}
1
]
)
10
2
*
2
/
2
^
2
%
2
-10
2
.
"_"
..c
2.
3.
4.
5.
6.
)
10
then
)
9
then
)
)
i
do
..i
)
)
"himi1"
"himi2"
"himi3"
"himi4"
"himi5"
}
..k..
"value:"
..v
)
20
true
do
25
then
break
..w
)
20
aa
2
..aa
)
>
28
10
20
)
<
then
)
)
)
)
then
)
)
关于函数
--函数详解
aCount
)
aCount
a
)
)
)
" and "
..b
)
)
;
...
)
)
day1
--[[function fuc(n)
if n==0 then
return 1
else
return n*fuc(n-1)
end
end
print("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}
end
local 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","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
end
a = {1,2,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
end
print(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
end
People.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
end
Man = {}
Man.new = function(name)
local self = People.new(name)
copy(self,Man)
return self
end
Man.sayHello = function()
print("Man say hello")
end
Man.sayhi = function(self)
print("hi "..self.name)
end
local m= Man.new("人")
m.sayhi(m)
m:sayHello()
--m:sayhi(m)
--可变参数
function show(...)
local num = select('#',...)
if num>0 then
print("恩")
else
print("无")
end
end
show()
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' thenreturn 10endreturn nilend,__newindex = function(tb,v) --这个是用于设置值print("__newindex key:" .. k)end}]]setMetatable(tbB,tbA)tbB.a = 3print(tbB.a)--tbB.d = 3--tbB.b = 5--print(tbB.d)