建造_船坞 发表于 2012-12-10 13:52:37

黑马程序员----c#函数重载

黑马程序员----c#函数重载

<div id="cnblogs_post_body">
c#方法重载:一个类中可以有一个以上名称相同的方法。但它们的签名不同,也就是括号里面的参数不同。仅方法返回值不同,是不能重载的。也就是方法重载与返回值无关。
常用的两种重载:
      public static int compute(int x, int y)
      {
            return x + y;
      }
      //方法参数个数不同
      public static int compute(int x, int y, int z)
      {
            return x + y + z;
      }
      //方法参数类型不同
      public static double compute(double x, double y)
      {
            return x + y;
      }
调用上面的方法:
      static void Main(string[] args)
      {
            Console.WriteLine(compute(3, 2));
            Console.WriteLine(compute(3.1,2.1));
            Console.WriteLine(compute(3, 2, 1));
            Console.ReadKey();
      }
输出结果:5,5.2,6
页: [1]
查看完整版本: 黑马程序员----c#函数重载