介绍
在本文中,我们将学习 C# 中的构造函数和构造函数类型。C# 中有五种不同类型的构造函数。构造函数用于创建类的对象。以下是 C# 中的构造函数列表。
- 默认构造函数
- 参数化构造函数
- 复制构造函数
- 静态构造函数
- 私有构造函数
什么是构造函数?
构造函数是一种用于初始化对象的特殊方法。构造函数在创建对象时调用。构造函数名称必须与其类名相同。构造函数不得有显式返回类型。
构造函数和方法的区别
默认构造函数
没有任何参数的构造函数称为默认构造函数。如果我们不创建构造函数,则类将在创建对象时自动调用默认构造函数。
例子
using System;
namespace DefaultConstructor_Demo
{public class Customer{public string firstName;public string lastName;public Customer(){}}class Program{static void Main(string[] args){Customer customer = new Customer();customer.firstName = "Farhan";customer.lastName = "Ahmed";Console.WriteLine("Full Name: " + customer.firstName + " " + customer.lastName);Console.ReadLine();}}
}
参数构造函数
至少具有一个参数的构造函数称为参数化构造函数。
例子
using System;
namespace ParameterConstructor_Demo
{class ParameterConstructor{public int FirstNumber;public int SecondNumber;public ParameterConstructor(int firstNumber, int secondNumber){FirstNumber = firstNumber;SecondNumber = secondNumber;}}class Program{static void Main(string[] args){ParameterConstructor p = new ParameterConstructor(10, 20);int Result = p.FirstNumber + p.SecondNumber;Console.WriteLine("Total:" + Result);Console.ReadLine();}}
}
复制构造函数
通过从另一个对象复制变量来创建对象的构造函数称为复制构造函数。
例子
using System;
namespace CopyConstructor_Demo
{public class Employee{public string firstName;public string lastName;public string position;public int salary;public Employee(){}// Copy constructor.public Employee(Employee employee){firstName = employee.firstName;lastName = employee.lastName;position = employee.position;salary = employee.salary;}}class Program{static void Main(string[] args){Employee emp = new Employee();Employee emp1 = new Employee(emp);Console.WriteLine("Enter your first name:");emp1.firstName = Convert.ToString(Console.ReadLine());Console.WriteLine("Enter your last name:");emp1.lastName = Convert.ToString(Console.ReadLine());Console.WriteLine("Enter your position:");emp1.position = Convert.ToString(Console.ReadLine());Console.WriteLine("Enter your salary:");emp1.salary = Convert.ToInt32(Console.ReadLine());Console.WriteLine("First Name:" + emp1.firstName);Console.WriteLine("Last Name:" + emp1.lastName);Console.WriteLine("Position:" + emp1.position);Console.WriteLine("Salary:" + emp1.salary);}}
}
静态构造函数
静态构造函数用于初始化任何静态数据或执行只需执行一次的特定操作。它在创建第一个实例或引用任何静态成员之前自动调用。
静态构造函数的特点
- 静态构造函数不接受任何访问修饰符。
- 静态构造函数没有参数。
- 在创建第一个实例或引用任何静态成员之前,会自动调用静态构造函数来初始化类。
- 静态构造函数不能被直接调用。
- 用户无法控制程序中静态构造函数的执行时间。
- 静态构造函数的典型用途是当类使用日志文件时,构造函数用于将条目写入该文件。
- 一个类只能有一个静态构造函数。
- 它只能访问类的静态成员。
例子
using System;
namespace StaticConstructor_Demo
{public class Customer{public string firstName;public string lastName;public static string discount;public Customer(string FirstName, string LastName){firstName = FirstName;lastName = LastName;}static Customer(){discount = 10 + "%";}public void CustomerDetails(){Console.WriteLine("Full Name:{0}", firstName + " " + lastName);Console.WriteLine("Discount:{0}", discount + "\n");}}class Program{static void Main(string[] args){Customer c1 = new Customer("Farhan", "Ahmed");Customer c2 = new Customer("Abdul", "Jabbar");c1.CustomerDetails();c2.CustomerDetails();Console.ReadLine();}}
}
私有构造函数
私有构造函数是一种特殊的实例构造函数。它通常用于仅包含静态成员的类中。如果一个类有一个或多个私有构造函数,而没有公共构造函数,则其他类(嵌套类除外)无法创建此类的实例。私有构造函数的用途是为单例类提供服务。单例类是将创建的对象数量限制为一个的类。使用私有构造函数,我们可以确保一次只能创建一个对象
- 私有构造函数的一个用途是当我们只有一个静态成员时。
- 它提供了单例类模式的实现。
- 一旦我们提供了一个构造函数(private/public/any),编译器就不会将无参数的公共构造函数添加到任何类中。
例子
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PrivateConstructor_Demo
{public class Candidate{private Candidate(){}public static int CandidateVisitedForInterview;public static int CountCandidate(){return ++CandidateVisitedForInterview;}}class Program{static void Main(string[] args){// The following comment line will throw an error because constructor is inaccessible//Candidate candidate = new Candidate();Candidate.CandidateVisitedForInterview = 20;Candidate.CountCandidate();Console.WriteLine("Interviewed candidates: {0}", Candidate.CandidateVisitedForInterview);Console.ReadLine();}}
}