table_test =setmetatable({66,99,77}, { __add = function(tb1,tb2) for k,v inpairs(tb2) do table.insert(tb1,k,v) end end })
table3 = table_test+{33,44,11}
for k,v inpairs(table_test) do print(k,v) end
重载元表的 __call 与 __tostring
1 2 3 4 5 6 7 8 9 10 11 12 13 14
table_test =setmetatable({66,99,77}, { __call = function(tb1,tb2) for k,v inpairs(tb2) do table.insert(tb1,k,v) end end })
table_test({33,44,11})
for k,v inpairs(table_test) do print(k,v) end
1 2 3 4 5 6 7 8 9 10 11
table1 = setmetatable({33,44,11},{ __tostring = function(tb1) total = 0 for k,v inpairs(tb1) do total = total + v end return"value total >> " .. total .. "<<<" end })
-- coroutine_test.lua 文件 co = coroutine.create( function(i) print(i); end ) coroutine.resume(co, 1) -- 1 print(coroutine.status(co)) -- dead print("----------") co = coroutine.wrap( function(i) print(i); end ) co(1) print("----------") co2 = coroutine.create( function() for i=1,10do print(i) if i == 3then print(coroutine.status(co2)) --running print(coroutine.running()) --thread:XXXXXX end coroutine.yield() end end ) coroutine.resume(co2) --1 coroutine.resume(co2) --2 coroutine.resume(co2) --3 print(coroutine.status(co2)) -- suspended print(coroutine.running()) print("----------")