方法的重载
- 方法重载:方法重载是指在同一个类中方法同名,参数不同(参数的类型、个数不同),调用时根据实参的形式,选择与他匹配的方法执行操作的一种技术
- 参数个数和类型都⼀样,但返回值类型不⼀样,不能算做重载
方法的递归
- 方法自己调用自己
- 多个⽅法之间来回调⽤
- 使⽤递归时⼀定要有出口
- 示例:斐波那契数列
class MathTool
{
//斐波那契数列
//0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …
public long GetFib(long index)
{
//如果编号小于1,则错误
if (index < 1)
{
return -1;
}
if (index == 1)
{
return 0;
}
if (index == 2)
{
return 1;
}
return GetFib(index - 1) + GetFib(index - 2);
}
}
class MainClass
{
public static void Main(string[] args)
{
MathTool ml = new MathTool();
long result = ml.GetFib(30);
Console.WriteLine(result);
}
}
得到结果