–print(“hello world”)
–
–函数
–与c java相同传入的是值
function method(a,b)
temp = a
a=b
b=temp
print(a,b)
end
–
–数组(字典)
–array[1] 的时候无法使用array.1
array ={}
array[“key”] = “fa”
print(array.key)
–
–循环
– c中的do while
local i = 0
repeat
i = i+1
print(“repeat 循环”)
until(i>2)
–for
–for 步长
for var = 1,6,2.5 do
print(“for步长循环”)
end
–字典循环
days = {“faf”,”fdaee”,”ere”}
days[1] = “fdafafadfdafad”
for i,v in ipairs(days) do
print(i,v)
end
–判断
if(0) then
print(“0是true”)
end
–函数返回多个值
function m(a,b)
return b,a
end
c,d = m(1,3)
print(“函数的多个返回值”,c,d)
–逻辑运算符
–and or not
–每一个变量对应着 一个值,b = false
a,b = true
if a and b then
print(“a and b”)
end
if a or b then
print(“a or b”)
end
if a and not b then
print(“not a and b”)
end
–连接字符串
a=’hello ’ b=’miko’
print(a..b)
print(“#表示字符串长度”,#a)
–String 操作
str = “Hello WORLD”
–全部大小写
print(string.upper(str))
print(string.lower(str))
–替换
print(string.gsub(str,’llo’,’heh’))
print(string.find(str,’e’))
–反转
print(string.reverse(str))
–面向对象
– Meta class
Shape = {area = 0}
– 基础类方法 new
function Shape:new (o,side)
o = o or {}
setmetatable(o, self)
self.__index = self
side = side or 0
self.area = side*side;
return o
end
– 基础类方法 printArea
function Shape:printArea ()
print(“面积为 “,self.area)
end
– 创建对象
myshape = Shape:new(nil,10)
myshape:printArea()