无聊的时候用LUA模拟C++的函数重载(如有不足请指出)
paras = {
para1 = nil,
para2 = nil,
}
function printNumber(_a, _b)
if paras.para1 ~= nil and paras.para1 ~= nil then
print("number: " .. " para1 = " .. paras.para1 .. " para2 = " .. paras.para2)
end
end
function printBool(_a, _b)
if paras.para1 ~= nil and paras.para1 ~= nil then
print("bool: " .. " para1 = " .. tostring(paras.para1) .. " para2 = " .. tostring(paras.para2))
end
end
funcs = {
num = function() return printNumber() end,
bool = function() return printBool() end,
}
function adaFuc(_a, _b)
print(type(_a) .. " ".. tostring(_a) .. " " .. type(_b) .. " " .. tostring(_b))
if type(_a) == "number" and type(_b) == "number" then
paras.para1 = _a
paras.para2 = _b
funcs.num()
elseif type(_a) == "boolean" and type(_b) == "boolean" then
paras.para1 = _a
paras.para2 = _b
funcs.bool()
elseif type(_a) == "string" and type(_b) == "string" then
paras.para1 = _a
paras.para2 = _b
funcs.bool()
end
end
function test()
adaFuc(1,2)
adaFuc(true,true)
adaFuc("string","string")
end
test()