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

C# 构造函数重载

C# 构造函数重载

在本文中,您将通过示例了解 C# 中的构造函数重载。

在C#中,类似于方法重载,我们也可以重载构造函数。对于构造函数重载,必须有两个或多个同名但不同的构造函数

在学习构造函数重载之前,请务必了解 C# 构造函数。


我们可以通过以下方式执行构造函数重载:

1。参数个数不同

如果构造函数的参数个数不同,我们可以重载构造函数。

 class Car {   

  Car() {
    ...
  }

  Car(string brand) {
    ...
  }
    
  Car(string brand, int price) {
    ...
  }

}

在这里,我们在类 Car 中有三个构造函数 .可以有多个构造函数,因为构造函数中的参数数量不同。

请注意,

示例:不同数量参数的构造函数重载

using System;

namespace ConstructorOverload {

  class Car {   
    
    // constructor with no parameter
    Car() {
      Console.WriteLine("Car constructor");
    }
     
    // constructor with one parameter
    Car(string brand) {
      Console.WriteLine("Car constructor with one parameter");
      Console.WriteLine("Brand: " + brand);
    }

    static void Main(string[] args) {

      // call with no parameter
      Car car = new Car();

      Console.WriteLine();

      // call with one parameter 
      Car car2 =  new Car("Bugatti");
     
      Console.ReadLine();
    }
  }
}

输出

Car constructor

Car constructor with one parameter
Brand: Bugatti

在上面的例子中,我们重载了 Car 构造函数:

  1. 一个构造函数有一个参数
  2. 另一个有两个参数

根据构造函数调用时传入的参数个数,调用对应的构造函数。

在这里,


2.不同类型的参数

class Car {   

  Car(string brand) {
    ...
   }

  Car(int price) {
    ...
  }
}

在这里,我们有两个 Car 具有相同数量参数的构造函数。我们可以创建具有相同参数的构造函数,因为参数内部的数据类型不同。

请注意,

示例:使用不同类型参数的构造函数重载

using System;

namespace ConstructorOverload {

  class Car {   
    
    // constructor with string parameter
    Car(string brand) {
      Console.WriteLine("Brand: " + brand);
    }

    // constructor  with int parameter
    Car(int price) {
      Console.WriteLine("Price: " + price);
    }

    static void Main(string[] args) {

      // call constructor  with string parameter
      Car car = new Car("Lamborghini");
      
      Console.WriteLine();

      // call constructor  with int parameter
      Car car2 =new Car(50000);
    
      Console.ReadLine();
    }
  }
}

输出

Brand: Lamborghini

Price: 50000

在上面的程序中,我们用不同类型的参数重载了构造函数。

在这里,

  1. 对象汽车 - 使用 string 调用构造函数 类型参数
  2. 对象 car2 - 使用 int 调用构造函数 类型参数

3。参数顺序不同

Car {

  Car(string brand, int price) {
    ...
  }

  Car(int speed, string color) {
    ... 
  }

}

在这里,我们有两个具有相同数量参数的构造函数。这是可能的,因为参数中数据类型的顺序不同。

请注意,

示例:使用不同参数顺序的构造函数重载

using System;

namespace ConstructorOverload {

  class Car {   
    
    // constructor with string and int parameter
    Car(string brand, int price) {

      Console.WriteLine("Brand: " + brand);
      Console.WriteLine("Price: " + price);
    }
    
    // constructor with int and string parameter
    Car(int speed, string color) {
      
      Console.WriteLine("Speed: " + speed + " km/hr");
      Console.WriteLine("Color: " + color);
    }
    static void Main(string[] args) {

      // call constructor  with string and int parameter
      Car car = new Car("Bugatti", 50000);
      
      Console.WriteLine();

      // call constructor with int and string parameter
      Car car2 =new Car(60, "Red");
    
      Console.ReadLine();
    }
  }
}

输出

Brand: Bugatti
Price: 50000

Speed: 60 km/hr
Color: Red

在上面的程序中,我们重载了具有不同参数顺序的构造函数。

在这里,

  1. 对象汽车 - 使用 string 调用构造函数 和 int 参数分别
  2. 对象 car2 - 使用 int 调用构造函数 和 string 参数分别

C语言

  1. C# 构造函数
  2. C# 嵌套类
  3. C# 方法重载
  4. Python 运算符重载
  5. Java 构造函数
  6. Java枚举构造函数
  7. 什么是汽车发动机冷却液?
  8. 带有示例的 C++ 运算符重载
  9. Java中的构造函数重载:什么是&程序示例
  10. Java - 构造函数
  11. C++ 重载(运算符和函数)
  12. C# - 运算符重载