先决条件:C#中的构造函数
它与方法重载非常相似。它是一种以多种形式重新定义构造函数的功能。用户可以通过在一个共享相同名称的类中定义两个或多个构造函数来实现构造函数重载。 C#可以区分具有不同签名的构造函数。即,构造函数必须具有相同的名称,但具有不同的参数列表。
我们可以以不同的方式重载构造函数,如下所示:
- 通过使用不同类型的参数
- 通过使用不同数量的参数
- 通过使用不同顺序的参数
通过更改参数的数据类型
例子:
public ADD (int a, float b);
public ADD (string a, int b);
在这里,类的名称为ADD 。在第一个构造函数中有两个参数,第一个是int ,另一个是float ,在第二个构造函数中,也有两个参数,第一个是字符串类型,另一个是int类型。
此处的构造函数名称相同,但参数的类型不同,类似于方法重载的概念。
// C# program to Demonstrate the overloading of
// constructor when the types of arguments
// are different
using System;
class ADD {
int x, y;
double f;
string s;
// 1st constructor
public ADD(int a, double b)
{
x = a;
f = b;
}
// 2nd constructor
public ADD(int a, string b)
{
y = a;
s = b;
}
// showing 1st constructor's result
public void show()
{
Console.WriteLine("1st constructor (int + float): {0} ",
(x + f));
}
// shows 2nd constructor's result
public void show1()
{
Console.WriteLine("2nd constructor (int + string): {0}",
(s + y));
}
}
// Driver Class
class GFG {
// Main Method
static void Main()
{
// Creating instance and
// passing arguments
// It will call the first constructor
ADD g = new ADD(10, 20.2);
// calling the method
g.show();
// Creating instance and
// passing arguments
// It will call the second constructor
ADD q = new ADD(10, "Roll No. is ");
// calling the method
q.show1();
}
}
1st constructor (int + float): 30.2
2nd constructor (int + string): Roll No. is 10
通过更改参数数量
在这种情况下,我们将使用两个或多个具有不同数量参数的构造函数。参数的数据类型可以相同,但参数的数量将不同。
例子:
public ADD (int a, int b);
public ADD (int a, int b, int c);
在这里,类名是ADD 。在第一个构造函数中,参数的数量为2 ,参数的类型为int 。在第二个构造函数中,参数的数目为3 ,参数的类型也为int ,它们与数据类型无关。
// C# Program to illustrate the overloading of
// constructor when the number of parameters
// are different
using System;
class ADD {
int x, y;
int f, p, s;
// 1st constructor
public ADD(int a, int b)
{
x = a;
y = b;
}
// 2nd constructor
public ADD(int a, int b, int c)
{
f = a;
p = b;
s = c;
}
// showing 1st constructor's result
public void show()
{
Console.WriteLine("1st constructor (int + int): {0} ",
(x + y));
}
// showing 2nd constructor's result
public void show1()
{
Console.WriteLine("2nd constructor (int + int + int): {0}",
(f + p + s));
}
}
// Driver Class
class GFG {
// Main Method
static void Main()
{
// will call 1st constructor
ADD g = new ADD(10, 20);
// calling method
g.show();
// will call 2nd constructor
ADD q = new ADD(10, 20, 30);
// calling method
q.show1();
}
}
1st constructor (int + int): 30
2nd constructor (int + int + int): 60
通过更改参数的顺序
例子:
public student(double a, int x, string s)
public student(string s, int x, double a)
在这里,两个构造函数具有相同类型的参数,即每个构造函数具有一个double类型,一个int类型和一个字符串类型参数,但是参数的位置不同。编译器将根据其参数顺序调用构造函数。
// C# program to illustrate the overloading of
// constructor by changing the order of parameters
using System;
class student {
public int roll;
public double Height;
public string name;
public student(double a, int x, string s)
{
roll = x;
name = s;
Height = a;
}
// order of the argument is different
// with respect to 1st constructor
public student(string s, int x, double a)
{
Height = a;
roll = x;
name = s;
}
public void show()
{
Console.WriteLine("Roll Number: " + roll);
Console.WriteLine("Height: " + Height + "feet");
Console.WriteLine("Name: " + name);
}
}
// Driver Class
class Geeks {
// Main Method
static void Main()
{
// invoking 1st constructor
student s1 = new student(5.7, 10, "Jhon Peterson");
// invoking 2nd constructor
student s2 = new student("Peter Perker", 11, 6.0);
Console.WriteLine("First Constructor: ");
s1.show();
Console.WriteLine();
Console.WriteLine("Second Constructor: ");
s2.show();
}
}
First Constructor:
Roll Number: 10
Height: 5.7feet
Name: Jhon Peterson
Second Constructor:
Roll Number: 11
Height: 6feet
Name: Peter Perker
使用“ this”关键字调用一个重载的构造方法
我们可以用这个关键字从另一个构造函数调用重载构造函数,但构造函数必须是属于同一类,因为这个关键字指向在此使用同一类的成员。这种调用重载构造函数的类型也称为“构造函数链接” 。
例子:
Let the class name is gfg,
Now
public gfg()
public gfg(int a) : this()
public gfg(double b) : this(int)
在这里,第一个构造函数是默认构造函数,第二个和第三个构造函数是参数化构造函数,其中一个具有int类型,另一个具有double类型参数。
在第二个构造函数中, this()调用第一个构造函数,即默认构造函数。在此关键字之后,只有() ,表示编译器调用不带参数的构造函数,表示默认构造函数。
在第三个构造函数中, this(int)调用第二个构造函数,该第二个构造函数是参数化的constructor 。这里,在此之后有(INT),这意味着,编译器调用具有int类型参数构造。
// C# program to illustrate the invoking of
// overloaded constructor using this keyword
using System;
class GFG {
// Private data members
private int Length, Height;
private double Width;
// Default Constructor
public GFG()
{
Console.WriteLine("Default Constructor Invoked");
}
// The constructor calls the
// Default constructor
public GFG(int l, double w) : this()
{
Console.WriteLine("Parameterized Constructor in 2nd Constructor");
// assigning value of
// 'Length'and 'Width'
Length = l;
Width = w;
}
// The constructor call the
// parameterized constructor
public GFG(int l, double w, int h) : this(l, w)
{
Console.WriteLine("Parameterized Constructor in 3rd Constructor");
// assign value of 'Height'
Height = h;
}
// Public Method to calculate volume
public double Volume()
{
return (Length * Width * Height);
}
}
// Driver Class
class Geeks {
// Main Method
public static void Main()
{
// Invoking 3rd Constructor
// here Constructor chaining
// came into existence
GFG g = new GFG(10, 20.5, 30);
// calling the 'Volume' Method
Console.WriteLine("Volume is : {0}", g.Volume());
}
}
Default Constructor Invoked
Parameterized Constructor in 2nd Constructor
Parameterized Constructor in 3rd Constructor
Volume is : 6150
复制构造函数的重载
包含相同类类型参数的参数化构造函数称为复制构造函数。基本上,复制构造函数是将一个对象的数据复制到另一个对象的构造函数。它的主要用途是将新实例初始化为现有实例的值。
// C# program to illustrate the
// overloading of Copy Constructor
using System;
class GFG {
public string p1, p2;
public int p3, p4;
// 1st Constructor
public GFG(string x, string y)
{
p1 = x;
p2 = y;
}
// Copy Constructor of 1st Constructor
public GFG(GFG gf)
{
p1 = gf.p1;
p2 = gf.p2;
}
// 2nd Constructor with different
// types pf parameter
public GFG(int a, int b)
{
p3 = a;
p4 = b;
}
// Copy Constructor of 2nd Constructor
// Here number of parameter is different
// with respect to 1st Constructor
public GFG(GFG a, GFG b)
{
p3 = a.p3;
p4 = b.p4;
}
}
// Driver Class
class Geeks {
// Main Method
static void Main()
{
// Create instance to class 'GFG'
GFG g = new GFG("Welcome", "GeeksForGeeks");
// Here 'g' details will copied to 'g1'
GFG g1 = new GFG(g);
Console.WriteLine(g1.p1 + " to " + g1.p2);
// Create instance to class 'GFG'
// with different types of parameter
GFG G = new GFG(10, 20);
// Here 'G' details will copied to 'G1'
GFG G1 = new GFG(G, G);
Console.WriteLine("Overloaded values : {0} and {1}",
G1.p3, G1.p4);
}
}
Welcome to GeeksForGeeks
Overloaded values : 10 and 20
笔记:
- 静态构造函数不能重载,因为静态构造函数是无参数的构造函数,但是对于重载,我们必须需要参数化的构造函数。
- 私有构造函数可以重载,我们可以在同一类中的此类实例使用它。私人成员不能从班级外部访问。