C# 构造函数:类型、示例与高级模式完全指南
C# 构造函数
探索构造函数如何在 C# 中初始化对象,涵盖所有构造函数类型、模式与最佳实践。
在 C# 中,构造函数是在创建类的新实例时自动调用的特殊方法。与普通方法不同,构造函数:
- 与类同名
- 无返回值
创建 C# 构造函数
在类中定义与类同名的构造函数:
class Car {
// 构造函数
Car() {
// 初始化代码
}
}
Car() 即为构造函数。使用 new 关键字调用:
new Car();
例如:
Car car1 = new Car();
另请参阅:C# 类与对象。
构造函数类型
常见构造函数类别:
- 无参(默认)构造函数
- 带参构造函数
- 拷贝构造函数
- 私有构造函数
- 静态构造函数
1. 无参构造函数
无参数的构造函数称为无参或默认构造函数。示例:
using System;
namespace ConstructorDemo {
class Car {
// 无参构造函数
Car() {
Console.WriteLine("Car Constructor");
}
static void Main(string[] args) {
new Car();
Console.ReadLine();
}
}
}
输出
Car Constructor
2. 带参构造函数
构造函数可接受参数以初始化实例字段:
using System;
namespace ConstructorDemo {
class Car {
string brand;
int price;
// 带参构造函数
Car(string theBrand, int thePrice) {
brand = theBrand;
price = thePrice;
}
static void Main(string[] args) {
Car car1 = new Car("Bugatti", 50000);
Console.WriteLine("Brand: " + car1.brand);
Console.WriteLine("Price: " + car1.price);
Console.ReadLine();
}
}
}
输出
Brand: Bugatti Price: 50000
3. 默认构造函数
若省略构造函数,编译器提供隐式无参构造函数,将字段初始化为默认值。示例:
using System;
namespace ConstructorDemo {
class Program {
int a;
static void Main(string[] args) {
Program p1 = new Program();
Console.WriteLine("Default value of a: " + p1.a);
Console.ReadLine();
}
}
}
输出
Default value of a: 0
数值字段默认 0,字符串和对象默认 null。
4. 拷贝构造函数
拷贝构造函数通过复制另一对象状态创建新实例:
using System;
namespace ConstructorDemo {
class Car {
string brand;
// 主构造函数
Car(string theBrand) {
brand = theBrand;
}
// 拷贝构造函数
Car(Car c1) {
brand = c1.brand;
}
static void Main(string[] args) {
Car car1 = new Car("Bugatti");
Console.WriteLine("Brand of car1: " + car1.brand);
Car car2 = new Car(car1);
Console.WriteLine("Brand of car2: " + car2.brand);
Console.ReadLine();
}
}
}
输出
Brand of car1: Bugatti Brand of car2: Bugatti
5. 私有构造函数
将构造函数标记为 private 可防止外部实例化,适用于单例模式或工具类。
示例
using System;
namespace ConstructorDemo {
class Car {
private Car() {
Console.WriteLine("Private Constructor");
}
}
class CarDrive {
static void Main(string[] args) {
// Car car1 = new Car(); // 编译错误
Console.ReadLine();
}
}
}
错误:
error CS0122: 'Car.Car()' is inaccessible due to its protection level
注意:构造函数为 private 时,类所有成员应为 static 以便通过类名访问。
6. 静态构造函数
静态构造函数每类型运行一次,在任何实例或静态成员被访问前执行。不能直接调用,不可有参数。
using System;
namespace ConstructorDemo {
class Car {
static Car() {
Console.WriteLine("Static Constructor");
}
Car() {
Console.WriteLine("Default Constructor");
}
static void Main(string[] args) {
Car car1 = new Car();
Car car2 = new Car();
Console.ReadLine();
}
}
}
输出
Static Constructor Default Constructor Default Constructor
注意:每类仅有一个静态构造函数;无参数、无访问修饰符。
C# 构造函数重载
定义多个参数列表不同的构造函数,提供灵活的对象创建方式。示例:
using System;
namespace ConstructorOverloadDemo {
class Car {
Car() {
Console.WriteLine("Car constructor");
}
Car(string brand) {
Console.WriteLine("Car constructor with one parameter");
Console.WriteLine("Brand: " + brand);
}
static void Main(string[] args) {
Car car = new Car();
Console.WriteLine();
Car car2 = new Car("Bugatti");
Console.ReadLine();
}
}
}
输出
Car constructor Car constructor with one parameter Brand: Bugatti
编译器根据实参数量和类型选择匹配的构造函数。详见 C# 构造函数重载。
C语言