this:代表的是调用者本身即调用者的当前对象。
特点:
1.区别成员字段和局部变量
2.调用其它构造函数
class Student
{ //字段
public string name;
public int age;
public Student()
{
Console.WriteLine("执行默认构造函数了");
}
//构造函数的局部变量和字段相同,且作用域有重叠,所以需要有所区分。其实不区分也可以,但是有时候会出小问题。谁调用,this就代表他。
public Student(string name, int age):this() //调用此函数之前执行默认构造函数
{
this.name = name;
this.age = age;
Console.WriteLine("执行有参构造函数了");
}
}
internal class Mainclus
{
public static void Main(string[] args)
{
//student调用了Studnet(name,age)构造函数,那么构造函数了的this就代表他。
Student student = new Student("ldd",18);
}
}
static: