lua学习笔记

来源:互联网 发布:cf2.0优化 编辑:程序博客网 时间:2024/06/06 05:09

备注:部分参考笨木头的博客(http://www.benmutou.com/archives/1844),很不错。


1、

-- 获取table的最小值--function GetMin(theTable)--  myString="minValue=math.min("--  --  for i,v in pairs(theTable) do--    myString=string.format("%s%d%s",myString,v,",")--  end--  myString=string.sub(myString,1,string.len(myString)-1)--  myString=string.format("%s%s",myString,")")--  --  assert(loadstring(myString))()--  return minValue--end--字符串替换--myString="happy,hello,home"--myString=string.gsub(myString,"h%a+","GG",2)--print(myString)--    function dieDaiQi(t)--        local i = 0;--        return function()--            i = i + 1;            --            return t[i];--        end--    end--    function dieDaiQi(t)--        local i = 0;--        return function()--            i = i + 1;   --            if i>#t then --            return nil,nil--            end         --            return i,t[i];--        end--    end--    local t = {"fdsd", "445"};--    for i,value in dieDaiQi(t) do--        print(i..":"..value);--    end--local co=coroutine.create(function() print("hello") end)--coroutine.resume(co)--print(dofile("src/luaTex.lua"));--------元表的操作---------------local a={sex="boy"}local b={sex="girl"}local mt={}--加法操作mt.__add=function(a,b)  if a.sex=="boy" and b.sex=="girl" then    result="good"  elseif a.sex=="girl" and b.sex=="girl" then    result="so good"  else    result="shit"  end  return resultend--保护元表mt.__metatable="sorry,you can not change this model"--查找不到的方法操作--mt.__index=function(table,key)--  print("给不了你要的:"..key)--end--备胎属性mt.__index={firstName="handsome",name="you are a bad boy"}------------------------------------这就是__newindex的规则:--a.如果__newindex是一个函数,则在给table不存在的字段赋值时,会调用这个函数。--b.如果__newindex是一个table,则在给table不存在的字段赋值时,会直接给__newindex的table赋值。--用于监控更新--mt.__newindex=function(table,key,value)--  print(key.."不存在")--endlocal other={--我们给a的name赋值的时候,实际上是给other的name赋值了。  name="other name"}mt.__newindex=othersetmetatable(a,mt)setmetatable(b,mt)--print(getmetatable(a))local result=a+bprint(result)a.name="good boy..."print(a.name)--print(other.name)--使用rawget和rawset可以忽略掉元表的__index和__newindex作用,相当于并不存在__index和__newindex元方法print(rawget(a,"name"))rawset(a,"name","after name..")print(rawget(a,"name"))print("----------------------------分水线------------------------")print("\n\n")print("---------------只读table----------------")local readOnly=function(t)  local newt={}  local mt={    __index=t,    __newindex=function()      error("只能读,不能改哦")    end  }  setmetatable(newt,mt)  return newtendlocal days=readOnly({"星期一","星期二","星期日"})print(days[2])--使用rawset,则打破了限制rawset(days,2,"222")print(days[2])print("---------------只读table----------------")print("\n\n")print("----------------------------非局部变量和局部变量------------------------")--    -- 定义一个全局变量--    gName = "哎哟,很挫哦";--    -- 将当前全局环境重新设置为新的table--    setfenv(1, {g = _G});--    -- 输出值--    g.print(gName);--    -- 再次定义一个全局变量--    gName = "哎哟,有点错哦";--    -- 再次输出值--    g.print(gName);--    -- 输出原来的值--    g.print(g.gName);--    g.print("----(1)----")gName="哈哈"local newG={}setmetatable(newG,{__index=_G})setfenv(1,newG)for _,v in pairs(newG) do  print("**"..v)endgName="哈哈..."for _,v in pairs(newG) do  print("**"..v)endprint(gName)print("------(2)使用__index-------")local game=require("src/luaTex")game.say()

2、

--冒号的作用就是:定义函数时,给函数的添加隐藏的第一个参数self;调用函数时,默认把当前调用者作为第一个参数传递进去。--使用了冒号之后,就相当于我们刚刚使用点号时一样,只是我们不再需要显式地定义self参数以及主动地传递tmp_people参数。people={name="xm",age=25,--setInfo=function(self,n,a)--  self.name=n--  self.age=a--end}function people:setInfo(n,a)  self.name=n  self.age=aendfunction people:getInfo()  print("name:"..self.name.."    age:"..self.age)end--people.setInfo("datou",30)--people.getInfo()tmp_people=people--print(people.name)--tmp_people.name="xxx"--print(people.name)people=nilprint(tmp_people:getInfo())

3、

--面向对象People={x=0,y=0}function People:setPosition(x,y)  self.x=x  self.y=yendfunction People:new()  o={}--  setmetatable(o,{__index=self})--  进行优化,避免每次都创建元表    setmetatable(o,self)    self.__index=self  return oend--使用People进行new,所以self存的是Peoplelocal p1=People:new()local p2=People:new()p1:setPosition(11,22)p2:setPosition(33,44)print(p1.x.."   "..p1.y)print(p2.x.."   "..p2.y)--函数的重载function p1:setPosition(x,y)  print("p1的重写函数")endp1:setPosition(8,8)print(p1.x.."   "..p1.y)print("----------------------")--使用p1进行new,所以self存的是p1local p1_1=p1:new()p1_1:setPosition(88,99)print(p1_1.x.."   "..p1_1.y)

4、

--多继承--(1)多继承之在多个类(即table)中查找一个字段function search(classes,key)  for i=1,#classes do    local value=classes[i][key]    if value~=nil then      return value    end  endendlocal t1={name="xx"}local t2={name="oo",age=25}--print(search({t1,t2},"name"))print(search({t1,t2},"age"))--(2)多继承之创建继承多个类的子类function createExtendsClass(...)  local parents={...}  local child={}--设置类元素(也就是从多个继承类中查找一个元素)setmetatable(child,{__index=function(table,key)  return search(parents,key)end})--给类增加创建对象的方法function child:new()  o={}  setmetatable(o,child)  child.__index=child  return oend  return childendtt1={name="kitty"}function tt1:hello()  print("hello,everyone")endfunction tt1:new()  o={}  setmetatable(o,self)  self.__index=self  return oendtt2={name="tom"}function tt2:fire()  print("hello,start fire")endfunction tt2:new()  o={}  setmetatable(o,self)  self.__index=self  return oend--继承两个类的子类local extendsT=createExtendsClass(tt1,tt2)--子类对象local tmp_extend=extendsT:new()tmp_extend:hello()tmp_extend:fire()print(tmp_extend.name)print("-------------弱引用table----------------")--t={}--key2 = {name = "key2"}--t[key2] = 1--for i,v in pairs(t) do--print(i.name..":"..v)--endt = {};-- 给t设置一个元表,增加__mode元方法,赋值为“k”setmetatable(t, {__mode = "k"});-- 使用一个table作为t的key值key1 = {name = "key1"};t[key1] = 1;    --key值弱引用    key1 = nil;    --value值弱引用    --t[key1]=nil        --key和value值弱引用    -- setmetatable(t, {__mode = "kv"});   -- 又使用一个table作为t的key值key2 = {name = "key2"};t[key2] = 1;key2 = nil;-- 强制进行一次垃圾收集collectgarbage();for key, value in pairs(t) do  print(key.name .. ":" .. value);end


0 0