在c#中写入一些重载函数
public int Calc(){
return 100;
}
public int Calc(int a){
Debug.Log("int");
return a;
}
public float Calc(float a){
Debug.Log("float");
return a;
}
public string Calc(string a){
Debug.Log("string");
return a;
}
public int Calc(int a, int b){
return a+b;
}
public int Calc(int a, out int b){
b = 10;
return a+b;
}
重载函数在lua中当做成员函数直接调用即可。
local obj = Lesson()
print(obj:Calc())
print(obj:Calc(1))
print(obj:Calc(1.2))
print(obj:Calc("123"))
print(obj:Calc(10,1))
print(obj:Calc(10,nil))
通过打印可知,tolua对重载函数的精度支持不太好,因为lua中只有number一种数值类型,所以会分不清int和float,默认会取精度最大的来使用。
在tolua中使用out重载,固定套路是使用nil占位out参数,所以因为ref参数不能用nil占位,会造成lua分不清重载的现象,所以这里建议不要使用ref,特别是在重载函数时。