C#|方法重载详细介绍

2021年3月9日16:04:22 发表评论 837 次浏览

方法重载是实现多态的常见方法。它是一种以多种形式重新定义功能的能力。用户可以通过在共享相同名称的类中定义两个或多个函数来实现函数重载。 C#可以区分以下方法不同的方法签名。即, 这些方法可以具有相同的名称, 但在同一类中具有不同的参数列表(即, 参数的数量, 参数的顺序和参数的数据类型)。

  • 根据作为参数传递给方法的参数的数量和类型来区分重载的方法。
  • 你不能定义多个具有相同名称, 顺序和参数类型的方法。这将是编译器错误。
  • 区分重载方法时, 编译器不考虑返回类型。但是你不能声明具有相同签名和不同返回类型的两个方法。它将引发编译时错误。如果这两种方法具有相同的参数类型, 但返回类型不同, 则不可能。

为什么我们需要方法重载?

如果我们需要以不同的方式(即针对不同的输入)执行相同类型的操作。在下面描述的示例中, 我们对不同的输入进行加法运算。很难为单个动作找到许多不同的有意义的名称。

进行重载方法的不同方法-

方法重载可以通过更改来完成:

  1. 两种方法中的参数数量。
  2. 方法的参数的数据类型。
  3. 方法的参数顺序。

通过更改参数数

// C# program to demonstrate the function 
// overloading by changing the Number
// of parameters
using System;
class GFG {
  
     // adding two integer values.
     public int Add( int a, int b)
     {
         int sum = a + b;
         return sum;
     }
  
     // adding three integer values.
     public int Add( int a, int b, int c)
     {
         int sum = a + b + c;
         return sum;
     }
  
     // Main Method
     public static void Main(String[] args)
     {
  
         // Creating Object
         GFG ob = new GFG();
  
         int sum1 = ob.Add(1, 2);
         Console.WriteLine( "sum of the two "
                           + "integer value : " + sum1);
  
         int sum2 = ob.Add(1, 2, 3);
         Console.WriteLine( "sum of the three "
                           + "integer value : " + sum2);
     }
}

输出如下:

sum of the two integer value : 3
sum of the three integer value : 6

通过更改参数的数据类型

// C# program to demonstrate the function 
// overloading by changing the Data types
//  of the parameters 
using System;
class GFG {
  
     // adding three integer values.
     public int Add( int a, int b, int c)
     {
         int sum = a + b + c;
         return sum;
     }
  
     // adding three double values.
     public double Add( double a, double b, double c)
     {
         double sum = a + b + c;
         return sum;
     }
  
     // Main Method
     public static void Main(String[] args)
     {
  
         // Creating Object
         GFG ob = new GFG();
  
         int sum2 = ob.Add(1, 2, 3);
         Console.WriteLine( "sum of the three "
                           + "integer value : " + sum2);
         double sum3 = ob.Add(1.0, 2.0, 3.0);
         Console.WriteLine( "sum of the three "
                           + "double value : " + sum3);
     }
}

输出如下:

sum of the three integer value : 6
sum of the three double value : 6

通过更改参数的顺序

// C# program to demonstrate the function 
// overloading by changing the 
// Order of the parameters
using System;
class GFG {
  
     // Method
     public void Identity(String name, int id)
     {
  
         Console.WriteLine( "Name : " + name + ", "
                           + "Id : " + id);
     }
      
     // Method
     public void Identity( int id, String name)
     {
  
         Console.WriteLine( "Name : " + name + ", "
                           + "Id : " + id);
     }
  
     // Main Method
     public static void Main(String[] args)
     {
  
         // Creating Object
         GFG obj = new GFG();
  
         obj.Identity( "Akku" , 1);
         obj.Identity( "Abby" , 2);
     }
}

输出如下:

Name : Akku, Id : 1
Name : Abby, Id : 2

当方法签名相同且返回类型不同时会发生什么?

编译器将给出错误, 因为仅返回值不足以使编译器找出必须调用的函数。仅当两个方法都具有不同的参数类型(因此, 它们具有不同的签名)时, 方法重载才是可能的。

例子:

// C# program to show error when method signature
// is the same and the return type is different.
using System;
class GFG {
  
     // adding two integer value.
     public int Add( int a, int b)
     {
  
         int sum = a + b;
         return sum;
     }
  
     // adding three integer value.
     public double Add( int a, int b)
     {
         double sum = a + b + 0.0;
         return sum;
     }
  
     // Main Method
     public static void Main(String[] args)
     {
  
         // Creating Object
         GFG ob = new GFG();
  
         int sum1 = ob.Add(1, 2);
         Console.WriteLine( "sum of the two "
                           + "integer value :" + sum1);
  
         int sum2 = ob.Add(1, 2);
         Console.WriteLine( "sum of the three "
                           + "integer value :" + sum2);
     }
}

编译时错误:

prog.cs(15, 19):错误CS0111:已经定义了成员GFG.Add(int, int)。重命名此成员或使用不同的参数类型prog.cs(7, 16):(与先前错误相关的符号位置)


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: