构造函数在面向对象编程中起着非常重要的作用。让我们通过以下几点来了解构造函数在 C# 中的作用:
语法:
public class Student() { //constructor public Student() { //code } }
这里,public Student() 是一个没有任何返回类型,甚至没有 void 的方法,它的名称与类名相同,即“Student”。因此,该方法是该类的构造函数。
当我们使用以下方法创建此类的对象时:
Student obj = new Student();
然后将执行构造函数内的代码。
1. 构造函数初始化新对象的数据成员。在内存分配给新对象后,它会立即由“new”运算符调用。
2. 显式构造函数(用户定义的构造函数)可以是无参数的,也可以是参数化的。如果它是参数化的,那么传递给构造函数的值可以分配给类的数据成员。
3. 即使我们创建该类的多个实例,隐式构造函数也会用相同的值初始化该类的变量。
示例:
代码:
using System; public class ConstructorDemo { public int num = 10; public static void Main() { ConstructorDemo obj1 = new ConstructorDemo(); ConstructorDemo obj2 = new ConstructorDemo(); ConstructorDemo obj3 = new ConstructorDemo(); Console.WriteLine("obj1.num = "+obj1.num+"\nobj2.num = "+obj2.num +"\nobj3.num = "+obj3.num); } }
输出:
上述程序的图示:
4. 带参数的显式构造函数允许我们在每次创建该类的实例时使用不同的值初始化该类的变量。
示例:
代码:
using System; public class ConstructorDemo { public int num; //explicit constructor public ConstructorDemo(int num) { this.num = num; } public static void Main() { ConstructorDemo obj1 = new ConstructorDemo(10); ConstructorDemo obj2 = new ConstructorDemo(20); ConstructorDemo obj3 = new ConstructorDemo(30); Console.WriteLine("obj1.num = "+obj1.num+"\nobj2.num = "+obj2.num +"\nobj3.num = "+obj3.num); } }
输出:
上述程序的图示:
C#提供了五种类型的构造函数。它们如下:
示例:
代码:
using System; public class DefaultConstructor { public int num; public string str; } public class Demo { public static void Main() { DefaultConstructor obj = new DefaultConstructor(); Console.WriteLine("obj.num = "+obj.num+"\nobj.str = "+obj.str); } }
输出:
具有至少一个参数的构造函数称为参数化构造函数。创建类的实例时可以将参数传递给构造函数。它允许我们用不同的值初始化类的每个实例。
示例:
代码:
using System; public class ParameterizedConstructor { public int num; public string str; //parameterized constructor public ParameterizedConstructor(int num, string str) { this.num = num; this.str = str; } } public class Demo { public static void Main() { //passing values to constructor while creating instance ParameterizedConstructor obj = new ParameterizedConstructor(50, "constructor"); Console.WriteLine("obj.num = "+obj.num+"\nobj.str = "+obj.str); } }
输出:
它是一个参数化构造函数,以同一个类的对象作为参数。它将现有对象的值(作为参数传递)复制到构造函数实例化的新创建的对象。我们可以说它将一个对象的数据复制到另一个对象。
示例:
代码:
using System; public class CopyConstructor { public int num; public CopyConstructor(int num) { this.num = num; } //copy constructor public CopyConstructor(CopyConstructor obj) { num = obj.num; } } public class Demo { public static void Main() { CopyConstructor obj1 = new CopyConstructor(50); //passing same class's object as parameter CopyConstructor obj2 = new CopyConstructor(obj1); Console.WriteLine("Original object:"); Console.WriteLine("obj1.num = "+obj1.num); Console.WriteLine("\nCopied object:"); Console.WriteLine("obj2.num = "+obj2.num); } }
输出:
示例:
代码:
using System; public class StaticConstructor { //static constructor static StaticConstructor() { Console.WriteLine("Static constructor executed"); } public static void Display() { Console.WriteLine("\nDisplay method executed"); } } public class Demo { public static void Main() { StaticConstructor.Display(); } }
输出:
A constructor created with a private specifier is called a private constructor. We cannot create an instance of the class if it contains only a private constructor, and it does not allow other classes to derive from this class. Used in class that contains only static members.
Example:
Code:
using System; public class PrivateConstructor { public static int num = 100; //private constructor private PrivateConstructor() { } } public class Demo { public static void Main() { //PrivateConstructor obj = new PrivateConstructor(); //Error Console.WriteLine("num = "+PrivateConstructor.num); } }
Output:
If we define any type of constructor in a class, then there will not be any implicit constructor in the class provided by the compiler. Like methods, parameterized constructors can also be overloaded with different numbers of parameters. Constructors defined implicitly by the compiler are always public.
This is a guide to Constructor in C#. Here we discuss the types of Constructor in C# and its Work along with Code Implementation and Output. You can also go through our other suggested articles to learn more –
以上是C# 中的构造函数的详细内容。更多信息请关注PHP中文网其他相关文章!