Lua函數進階 - 下


1 閉包

 

 

 

--閉包
--無參數閉包
function Func()         --local i 相當於Java中的靜態變量
    local i = 0;        --i 變量,稱為內嵌函數的 "upValue",這里的i變量,既不是全局變量,也不是局部變量. 這里稱為非局部變量
    print("---A--i: "..i)
    return function()   --內嵌入的匿名函數
        print("---B--i: "..i)
        i = i + 1
        print("---C--i: "..i)
        return i;
    end
end
--調用測試
f1 = Func()
print(f1())
print(f1())

 

2 帶參閉包

--帶參閉包
function FunWithPara(i)     --參數i 是內嵌函數的"UpValue"
    print("---A--i: "..i)
    return function()       --內嵌匿名函數
        print("---B--i: "..i)
        i = i + 1
        print("---C--i: "..i)
        return i
    end
end
--調用測試
fun2 = FunWithPara(10)
print(fun2())
print(fun2())

 

3 多內嵌函數閉包

 

--具備多個內嵌函數的閉包
function FunMuTil()
    local numUpValue = 10   --這是所謂的upValue
    function InnerFun1()
        print(numUpValue)
    end

    function InnerFun2()
        numUpValue = numUpValue + 100
    end
        return InnerFun1,InnerFun2
end

--調用測試
local res1,res2 = FunMuTil()
res1()      --  output:10
res2()      -- output:
res1()      -- output:110

4 帶參數的內嵌函數

--帶參數的內嵌函數
function Fun4(num)
    return function(value)      --內嵌函數帶有參數
        --return num * value    --這種寫法不能保存num自身狀態
        num = num * value
        return num
    end
end
--調用測試
fun4 = Fun4(10)
print(fun4(2))      --output: 20
print(fun4(2))      --output: 40

 

 

 

5 閉包具備多個實例

--閉包可以有多個實例
function Func5()
    local i = 0;
    return function()
        i = i + 1
        return i;
    end
end
--調用測試
res51 = Func5()
print(res51())   --output: 1
print(res51())   --output: 2
print(res51())   --output: 3

res52 =Func5()
print(res52())   --output: 1
print(res52())   --output: 2
print(res52())   --output: 3

6 自定義迭代器

--使用閉包,開發自己的迭代器
function Itr(table)
    local i = 0
    return function()
        i=i+1
        return table[i]
    end
end
--定義一個表
myTab = {1,22,333,4444,55555}
--迭代輸出
iterator = Itr(myTab)
while true do
    local j = iterator()
    if (j==nil) then
        break
    end
    print("j : "..j)
end

for m in Itr(myTab) do
    print("for : ",m)
end

 

7 模塊

 

 

7.1 新建一個lua文件

--模塊,本質上就是定義一個表
--定義局部模塊
local myModel={}

--定義模塊中的字段
myModel.gHeight=180
--定義模塊中的函數
--模塊中不能定義私有的函數與字段,否則無法調用
function myModel.Fun1()
print("Fun1 Method invoked !")
end

function myModel.Fun2()
print("Fun2 Method invoked !")
end

return myModel

 

7.2 在別的文件中調用

--lua文件的相互調用 關鍵字require
local model = require("LuaMiddle.C1_MyModel")
if (not model) then
    print("model 不存在")
    return
end
--模塊的調用
model.Fun1()    --測試模塊中的函數
model.Fun2()
print("模塊中的字段 : "..model.gHeight)

 

8 函數的前置聲明

--函數的前置聲明
local fun1 , fun2

function fun1()
    print"---Fun1---"
end
function fun2()
    print("--Fun2--")
end

fun1()
fun2()

 

9 unpack函數

unpack( )函數是接受一個數組來作為輸入參數,並默認從下標為1開始返回所有元素
--unpack函數,在5.3版本中 unpack() 應改為 table.unpack() 調用
myTab = {1,2,3,4,5,6,nil,0}
print(table.unpack(myTab))

 

--模塊,本質上就是定義一個表--定義局部模塊local myModel={}
--定義模塊中的字段myModel.gHeight=180--定義模塊中的函數--模塊中不能定義私有的函數與字段,否則無法調用function myModel.Fun1()print("Fun1 Method invoked !")end
function myModel.Fun2()print("Fun2 Method invoked !")end
return myModel


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM