亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> C语言

C# 方法重载

C#方法重载

在本文中,您将通过示例了解 C# 中的方法重载。

在C#中,一个类中可能有两个或多个同名但参数数量、类型和顺序不同的方法,称为方法重载。例如:

void display() { ... }
void display(int a) { ... }
float display(double a) { ... }
float display(int a, float b) { ... }

这里,display() 方法被重载。这些方法同名但接受不同的参数。

注意 :上述方法的返回类型不一样。这是因为方法重载与返回类型无关。重载的方法可能有相同或不同的返回类型,但它们必须有不同的参数。


我们可以通过以下方式进行方法重载:

1。通过改变参数个数

如果方法中的参数个数不同,我们可以重载方法。

void display(int a) {
  ...
} 
...
void display(int a, int b) {
  ...
} 

在这里,我们在一个类中有两个同名方法 - display() .由于方法中的参数数量不同,因此可能有多个同名方法。例如,

using System;

namespace MethodOverload {

  class Program {  

    // method with one parameter
    void display(int a) {
      Console.WriteLine("Arguments: " + a);
    }
 
    // method with two parameters
    void display(int a, int b) {
      Console.WriteLine("Arguments: " + a + " and " + b);
    } 
    static void Main(string[] args) {

      Program p1 = new Program();
      p1.display(100);
      p1.display(100, 200);
      Console.ReadLine();
    }
  }
}

输出

Arguments: 100
Arguments: 100 and 200

在上面的例子中,我们重载了 display() 方法:

根据方法调用时传递的参数个数,调用对应的方法。


2。通过改变参数的数据类型

void display(int a) {
  ...
} 
...
void display(string b) {
  ...
} 

在这里,我们有两个方法 - display() 具有相同数量的参数。可以有多个 display() 方法的参数个数相同,因为方法中参数的数据类型不同。例如,

using System;

namespace MethodOverload {

  class Program {  

    // method with int parameter
    void display(int a) {
      Console.WriteLine("int type: " + a);
    } 

    // method with string parameter
    void display(string b) {
      Console.WriteLine("string type: " + b);
    } 
    static void Main(string[] args) {

      Program p1 = new Program();
      p1.display(100);
      p1.display("Programiz");
      Console.ReadLine();
    }
  }
}

输出

int type: 100
string type: Programiz

在上面的程序中,我们重载了 display() 不同类型参数的方法。

根据方法调用过程中传递的参数类型,调用对应的方法。


3。通过改变参数的顺序

void display(int a, string b) {
  ...
} 
...
void display(string b, int a) {
  ...
}

在这里,我们有两个方法 - display() .可以有多个 display() 由于方法中参数的数据类型顺序不同,因此参数数量和类型相同的方法。例如,

using System;

namespace MethodOverload {

  class Program {  

    // method with int and string parameters 
    void display(int a, string b) {
      Console.WriteLine("int: " + a);
      Console.WriteLine("string: " + b);
    } 

    // method with string andint parameter
    void display(string b, int a) {
      Console.WriteLine("string: " + b);
      Console.WriteLine("int: " + a);
    } 
    static void Main(string[] args) {

      Program p1 = new Program();
      p1.display(100, "Programming");
      p1.display("Programiz", 400);
      Console.ReadLine();
    }
  }
}

输出

int: 100
string: Programming
string: Programiz
int: 400

在上面的程序中,我们重载了 display() 不同阶参数的方法。

根据方法调用过程中传递的参数的顺序,调用相应的方法。


C语言

  1. C#抽象类和方法
  2. C# 部分类和部分方法
  3. C# 密封类和方法
  4. C# 构造函数重载
  5. Python 运算符重载
  6. Java 注释类型
  7. Verilog 任务
  8. Verilog 显示任务
  9. C# - 方法
  10. C# - 运算符重载
  11. C# - 匿名方法
  12. 什么是 EPA 方法 21?