标签:
this关键字
1.首先一个用处就是代表当前类的对象。
2.当我们对构造函数进行重载的时候代码如下:
public class Class1
{
public string Name { get; set; }
public int Age { get; set; }
public string Grade { get; set; }
public int English { get; set; }
public int Math { get; set; }
public int Chinese { get; set; }
public Class1(string name,int age,string grade,int english,int math,int chinese)
{
this.Name = name;
this.Age = age;
this.Grade = grade;
this.English = english;
this.Math = math;
this.Chinese = chinese;
}
public Class1(string name,int english, int math, int chinese)
{
this.Name = name;
this.English = english;
this.Math = math;
this.Chinese = chinese;
}
public Class1(string name, int age)
{
this.Name = name;
this.Age = age;
}
public Class1()
{
}
}
以上代码完全没有问题,但是不免有些代码冗余的现象。我们可以使用this关键字
public class Class1
{
public string Name { get; set; }
public int Age { get; set; }
public string Grade { get; set; }
public int English { get; set; }
public int Math { get; set; }
public int Chinese { get; set; }
public Class1(string name,int age,string grade,int english,int math,int chinese)
{
this.Name = name;
this.Age = age;
this.Grade = grade;
this.English = english;
this.Math = math;
this.Chinese = chinese;
}
public Class1(string name,int english, int math, int chinese):this(name,0,"c",english,math,chinese)
{
//this.Name = name;
//this.English = english;
//this.Math = math;
//this.Chinese = chinese;
}
public Class1(string name, int age)
{
this.Name = name;
this.Age = age;
}
public Class1()
{
}
}
即this的第二个用处为在类中显式的调用本类的构造函数
标签:
原文地址:http://www.cnblogs.com/hunternet/p/4699909.html