在整理资料的时候找到这个资料,之前很早写的,上传下,下次有空的时候用来回忆
刚好前段时间比较空,把lua基础知识补充了下,之前
-------------------------- 2015年1月22日 --------------------------
-- 错误处理,程序终止,不会执行下去,会输出如下提示
-- error
--error("你的智商不多了,赶紧休息,恢复一下吧")
-- 如果assert的第一个参数为不为false,则返回第一个参数的值;否则,执行error函数,输出错误信息,错误信息的内容为assert的第二个参数。
--assert(false, "你是一个非常善良有爱心的人,我很喜欢你..所以,去死吧!")
-- 使用pcall调用test函数,如果test不报错,则pcall返回ture,否则,返回false。
function test()
print("dugaoda true")
end
if pcall(test) then
print("normal")
else
print("error")
end
-- 函数的几个特别之处 函数参数个数自适应
function dugaodamethod(sNmae, nAge)
print(sNmae)
print(nAge)
end
dugaodamethod("dugaoda")
dugaodamethod("dugdaoda", 40, 20, "hello")
function dugaodareturn()
return 10, 20
end
local a, b, c = 10, dugaodareturn() -- 输出 10 10 20
-- 函数的特点之一:如果函数的调用不是在表达式的最后一个元素,则最多只返回一个值
local a, b, c = dugaodareturn(), 10 -- 输出 10 10 nil
print(a)
print(b)
print(c)
-- 具名实参
function dugaodainfo1(args)
return args.name, args,age
end
-- 是根据table的key值来获取的,根本就不需要考虑参数顺序
local sname, nage = dugaodainfo1({name = "dugaoda", age = 30})
-- 非局部变量
function myclosure()
local i = 0 -- 在这里i属于非局部变量
return function ()
i = i + 1
return i
end
end
local mydiaoyong = myclosure() -- myclosure()函数的返回值是一个匿名函数,这里要特别的注意mydiaoyong对应的值是myclosure()的返回值匿名函数
-- 相当于 mydiaoyong = function () i = i + 1 return i end
-- 当执行 local mydiaoyong = myclosure() 的时候其实运行了myclosure()函数里面的内容对i赋了初0
-- 执行mydiaoyong()的时候其实相当于执行匿名函数里面的内容 返回得到i的内容
print(mydiaoyong()) -- 1
print(mydiaoyong()) -- 2
print(mydiaoyong()) -- 3
--在这里,local i就属于一个非局部变量,因为它既不是全局变量,也不是单纯的局部变量(因为另外一个函数可以访问到它)。
--再来回到定义,count函数里的那个函数,加上非局部变量i,就构成了一个闭合函数了,就这么简单。
--对于闭合函数而言,属于它的非局部变量,并不是在调用它的时候临时产生的,而是和它一起存在的。
--所以每次调用闭合函数,非局部变量的值都不会被重置。
function myclosure1()
return function ()
local ni = 123 -- 在这里ni属于局部变量
ni = ni + 1
return ni
end
end
local temp = myclosure1()
print(temp()) -- 输出123
print(temp()) -- 输出123
print(temp()) -- 输出123
-- 介绍一个“语法糖”
function dugaodayufa()
end
-- 等价于 上下两个等价
local dugaodayufa
dugaodayufa = function()
end
-- 这样就不会报错了 du1能够识别到du2
local du1
local du2
local function du1()
return du2()
end
local function du2()
print("23")
end
-- 尾调用
-- 尾调用的大致意思是:一个函数的调用是另一个函数的最后一个动作时,这个调用就称之为尾调用。
function foo1()
return foo()
end
--这就不属于尾调用,因为调用完count函数之后,还要取得count的返回值,然后进行一次加法操作
function foo2()
return foo() + 1
end
function foo234(n)
if n > 0 then
return foo234(n - 1)
else
return "dugaoda"
end
end
local p = foo234(5)
print(p)
-- 迭代器 打算2015年1月23日 看一遍
local tabTest = {du, gao, da, ha, ha, ha}
for index, value in ipairs(tabTest) do
print(value)
end
function mydiedaiqi(t)
local i = 0
return function()
i = i + 1
return t[i]
end
end
local iter = mydiedaiqi(tabTest)
-- 实现1
while true do
local value = iter()
if value == nil then
break
end
print(value)
end
-- 实现2
for value in mydiedaiqi(tabTest) do
print(value)
end
function a(...)
local arg = {...}
end
-- 字符串部分
-- require (用于使用模块)
-- module (用于创建模块)
print("require dugaoda")
--require("src/dugaoda")
require("src/dugaoda") --
-- 都可以正常的调用dugaoda.lua 中的MAX_DUGAODA_SIZE和hesls()变量和函数 ,如果dugaoda.lua中的变量或方法有关键字local那么就会调用不到
print(MAX_DUGAODA_SIZE)
hesls()
-- 包含模块文件
local dugaodason1 = require("dugaodason")
dugaodason1.info()
--pay()
-- 其实,require干的事就是这样:(这就是为什么模块文件要写成那样了)
--local dugaodason1 = (function()
---- dugaodason.lua的内容
--end)
-------------- 模块(module)和包(package) --------------
-- require会将返回值存储到table package.loaded中
require("dugaodabigson")
--pay() -- 错误 程序识别不到pay这个函数必须加上前缀dugaodabigson
dugaodabigson.pay() -- 正确
--简单的说
--a.lua
--module (..., package.seeall)
--flag=true
--function example()
--...
--end
--在别的脚本中
--require "a"
--调用脚本a中的任何全局变量或者函数必须加上a.
--这个好处就是你不同的脚本中可以用相同的名称定义变量或者函数,而不会混乱。
-------------- 模块(module)和包(package) --------------
-- Lua中的捕获机制和转换技巧介绍
local getstr = "dugaoda = memeda"
local _, _, key, vale = string.find(getstr, "(%a+)%s*=%s*(%a+)")
-- 存储哑元变量 _ 中
-- '%a+' 表示非空的字母序列;'%s*' 表示0个或多个空白 %[对应符号[ %]对应符号%] 貌似也可以用\[ \]
local date = "17/7/1990"
local month, day, year = string.find(date, "(%d+)/(%d+)/(%d+)")
-- 假定你想查找一个字符串中单引号或者双引号引起来的子串,你可能使用模式 '["'].-["']',
local s = [[then he said: "it's all right"!]]
-- Lua中handler方法的原理详解
print("-------------- handler --------------")
local dugaodahandler = {}
function dugaodahandler:fun()
self.m = "hello"
end
local dugaodahandler1 = {}
dugaodahandler.fun(dugaodahandler1) -- 因为fun函数里面执行的内容是 dugaodahandler1.m = "hello" 替代了self
print(dugaodahandler1.m) -- hello
print(dugaodahandler.m) -- nil
--handler(cls, cls.onClose)
function fandlertemp(obj, method)
return function(...)
return method(obj, ...)
end
end
local cls = {}
cls._m = "close"
function cls:onClose()
print(self._m)
end
fandlertemp(cls, cls.onClose)() -- 分析
--local sdie = fandlertemp(cls, cls.onClose) -- 结果sdie = function(...) return method(obj, ...) end
--sdie() -- 执行 method(obj, ...) 函数 cls:onClose() print(cls._m)
--+ 匹配前一字符1次或多次
--* 匹配前一字符0次或多次
--- 匹配前一字符0次或多次
--? 匹配前一字符0次或1次
-- 另外的2中循环
local conditon = 5
while conditon == 5 do
print("de")
conditon = 1
end
repeat
print("de")
conditon = 5
until conditon == 5
local di = {2,2,3}
local sd = next(di) -- 返回第一个索引的值 可以用next(t)来检测表是否为空(此函数只能用于以数字索引的表与ipairs相类似)
print(sd) -- 输出1
local myceil = math.ceil(3.1)
print(myceil) -- 输出4
--randomseed
--设随机数种子
math.randomseed(os.time())
--random
--取随机数
math.random(5,90)
5~90
--modf
取整数和小数部分
math.modf(20.12)
20 0.12
--mod
取模
math.mod(65535, 2)
1
local str1 = string.char(97) -- 转换整数为相对应的字符
print(str1)
-- Lua中实现面向对象的一种漂亮解决方案
local _class={}
function class(super)
local class_type = {}
class_type.ctor = false -- 设置构造函数
class_type.super = super -- 设置父类
class_type.new = function(...) -- 创建
local obj = {}
do
local create
create = function(c,...)
if c.super then
create(c.super, ...)
end
if c.ctor then
c.ctor(obj, ...)
end
end
create(class_type, ...)
end
setmetatable(obj, { __index = _class[class_type] })
return obj
end
local vtbl = {}
_class[class_type] = vtbl
setmetatable(class_type, {__newindex = function(t, k, v) vtbl[k] = v end })
if super then
setmetatable(vtbl,{__index = function(t,k) local ret=_class[super][k]
vtbl[k]=ret
return ret
end
})
end
return class_type
end
base_type = class() -- 定义一个基类 base_type
function base_type:ctor(a) -- 定义 base_type 的构造函数
print("base_type ctor")
self.m_a = a
end
function base_type:print_a() -- 定义一个成员函数
print(self.m_a)
end
function base_type:hello() -- 定义另一个成员函数 base_type:hello
print("hello base_type")
end
jichenglei = class(base_type) -- 定义一个类 jichenglei 继承于 basetype
function jichenglei:ctor() -- 定义 jichenglei 的构造函数
print("jichenglei ctor")
end
function jichenglei:hello() -- 重载 base_type:hello 为 jichenglei:hello
print("hello jichenglei")
end
myjicheng = jichenglei.new(2)
myjicheng:print_a()
print("class")