C#~方法重载

版权声明:盗版必究 https://blog.csdn.net/jinxiul5/article/details/81937999

什么是方法重载

方法重载实际上就是方法的多元化,当一个方法满足不了我们的需求的时候,需要再次添加多个方法,在进行调用的时候会很复杂。使用方法重载时根据实参的类型或者数量来确定使用哪个方法。
注意事项
<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 ( );
        }
    }
}

猜你喜欢

转载自blog.csdn.net/jinxiul5/article/details/81937999
今日推荐