在创建类的时候有3个Constructor,class Employee
{
// 1st
public Employee(){}
// 2nd
public Employee(string name, int id, float pay)
: this(name, 0, id, pay){}
// 3rd
public Employee(string Name, int Id, float Pay)
{
this.Name = Name;
this.ID = ID;
this.Pay = Pay;
}
private string Name;
private int ID;
private float Pay;}
谁能告诉我第二个constructor中 this的 作用, 如果用第二个Constructor会有什么结果,谢谢
{
// 1st
public Employee(){}
// 2nd
public Employee(string name, int id, float pay)
: this(name, 0, id, pay){}
// 3rd
public Employee(string Name, int Id, float Pay)
{
this.Name = Name;
this.ID = ID;
this.Pay = Pay;
}
private string Name;
private int ID;
private float Pay;}
谁能告诉我第二个constructor中 this的 作用, 如果用第二个Constructor会有什么结果,谢谢
在这里调用第二个实际调用的第三个。之所以用this调用构造函数的重载好处是:不同的构造函数在初始化类成员时都会“内联”方式初始化,而用this可以设置默认字段,减少生成的代码。