魏长东

weichangdong

lua setmetatable学习

 

local table1 = {
111
}

local table2 = {
111
}
local mt = {}
mt.__add = function(t1, t2)
    for _, item in ipairs(t2) do
        table.insert(t1, item)
    end
	return t1
end
setmetatable(table1,mt)
--setmetatable(table2,mt)

tSum = table2 + table1
--[[
for k, v in pairs(tSum) do
    print(v)
end
--]]


local old = {
	wcd = 'im wcd'
}

local new = {
	wcd1 = 'im new wcd',
	yy = 'im new yy'
}
setmetatable(old,{
__index = new
})

--print(old.wcd1)


local old_fun = {}
old_fun.wcd = 'wcd'
old_fun.fun = function()
	return 'im fun'
end

local new_old = {
	default = 'im default'
}

local new_fun = function()
	return 'im new fun'
end

setmetatable(old_fun,{
	__index = new_old,
	__call = new_fun
})

print(old_fun.default)

print(old_fun())

算是堆lua的setmatetable有了多一点的认识.