this关键字:
1、代表当前类的对象。
2、在类当中显示的调用本类的构造函数 :this
using System;
using System.Collections.Generic;
using System.Text;
namespace _051_面对对象初级_6_this关键字
{
class Student
{
public Student(string name, int age, char gender, int chinese, int math, int english)
{
this.Name = name;
this.Age = age;
this.Gender = gender;
this.Chinese = chinese;
this.Math = math;
this.English = english;
}
public Student(string name, int chinese, int math, int english) : this(name, 0, ' ', 100, 100, 100)
{
//this.Name = name;
//this.Chinese = chinese;
//this.Math = math;
//this.English = english;
}
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
private int _age;
public int Age
{
get { return _age; }
set
{
if (value < 0 || value > 100)
{
value = 0;
}
_age = value;
}
}
private char _gender;
public char Gender
{
get
{
if (_gender != '男' && _gender != '女')
{
return _gender = '×';
}
return _gender;
}
set { _gender = value; }
}
private int _chinese;
public int Chinese
{
get { return _chinese; }
set { _chinese = value; }
}
private int _math;
public int Math
{
get { return _math; }
set { _math = value; }
}
private int _english;
public int English
{
get { return _english; }
set { _english = value; }
}
public void SayHello()
{
Console.WriteLine("我叫{0},今天{1}岁了,是个{2}生", this.Name, this.Age, this.Gender);
}
public void ShouScore()
{
Console.WriteLine("我的总成绩是{0},平均成绩是{1}", (this.Chinese + this.Math + this.English), (this.Chinese + this.Math + this.English) / 3);
}
}
}