C# 构造函数重载

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

  • 不同的构造函数
  • 不同类型的参数
  • 不同顺序的参数

在学习构造函数重载之前,请确保您了解 C# 构造函数


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

1. 不同数量的参数

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

 class Car {   

  Car() {
    ...
  }

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

}

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

请注意:

  • Car() { } - 无参数
  • Car(string brand) { } - 有一个参数
  • Car(string brand, int price) { } - 有两个参数

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

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. 另一个带两个参数。

根据构造函数调用期间传递的参数数量,将调用相应的构造函数。

这里,

  • car 对象 - 调用带一个参数的构造函数
  • car2 对象 - 调用带两个参数的构造函数

2. 不同类型的参数

class Car {   

  Car(string brand) {
    ...
   }

  Car(int price) {
    ...
  }
}

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

请注意:

  • Car(string brand) { } - 参数类型为 string
  • Car(int price) { } - 参数类型为 int

示例:具有不同类型参数的构造函数重载

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. car 对象 - 调用带有 string 类型参数的构造函数
  2. car2 对象 - 调用带有 int 类型参数的构造函数

3. 不同顺序的参数

Car {

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

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

}

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

请注意:

  • Car(string brand, int price) { } - string 数据类型在 int 之前
  • Car(int speed, string color) { } - int 数据类型在 string 之前

示例:具有不同参数顺序的构造函数重载

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. car 对象 - 分别调用带有 stringint 参数的构造函数
  2. car2 对象 - 分别调用带有 intstring 参数的构造函数
你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战