Lua基础语句总结

–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()

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值