什么是方法重载
方法重载实际上就是方法的多元化,当一个方法满足不了我们的需求的时候,需要再次添加多个方法,在进行调用的时候会很复杂。使用方法重载时根据实参的类型或者数量来确定使用哪个方法。
注意事项:
<1>方法名必须相同。
<2>参数不同(参数数量不同,参数类型不同)。
<3>方法重载与返回值无关。
namespace ConsoleApp1
{
public class Math
{
//计算器 求和
public int Maths(int a,int b)
{
int c = a + b;
return c;
}
//计算器 求差
public int Maths(int a ,int b,int d)
{
int c = a - b - d;
return c;
}
}
class Program
{
static void Main(string[] args)
{
Math m = new Math ( );
Console.WriteLine (m.Maths (4, 5));
Console.ReadKey ( );
}
}
}