this 关键字
1)代表当前类的对象;
2)在类当中现实的调用本类的构造函数(语法为:this())
public Student(string name,int age,char gender,int chinese)//第一个构造函数
{
this.Name=name;
this.Age=age;
this.Gender=gender;
this.Chinese=chinese;
}
public Student(string name,int age,char gender):this(name,age,gender,0)//调用上一个构造函数,其中对于上面一个全参的构造函数来说,最后一个参数没有,就赋值为0
{
//调用上面的构造函数,巧用“this”不用写冗余代码
}
本文深入解析了this关键字在类中代表当前对象及如何通过this调用类的构造函数,避免代码冗余,展示了构造函数中this的实用案例。
1049

被折叠的 条评论
为什么被折叠?



