//People.cs public class People { //字段 private string _name; //属性 作用:保护字段,对字段的取值和赋值进行限定,限制非法字段的摄入 public string Name { //当你输出属性的值的时候执行get方法 get { return _name; } //当给属性赋值的时候,首先会执行set方法 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; } } //方法 public void CHLSS() { Console.WriteLine("我叫{0},我今年{1}岁了,我是{2}生,我很厉害。", this._name, this._age, this.Gender); } //public static void CHLSS1() //{ //} //program.cs class Program { static void Main(string[] args) { //创建类的对象 People renXiao = new People(); renXiao.Name = "任晓"; renXiao.Age = -23; renXiao.Gender = '欻'; renXiao.CHLSS(); Console.ReadKey(); } }