using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 面向对象 { //创建person类 public class person { //Fields:字段 private string _name; private int _age; private char _gender; //Properties:属性 public string Name { get { return this._name; } //获取属性值时,调用get方法 set { this._name = value; } //给属性赋值时,调用set方法 } public int Age { get { return this._age; } // 对set属性进行限定 set { if (value < 0 || value > 100) { value = 0; } this._age = value; } } public char Gender { //对get属性进行限定 get { if (_gender != '男' && _gender != '女') { return _gender = '男'; } else { return _gender; } } set { _gender = value; } } //Methods:方法 (行为) public void CHLSS() { Console.WriteLine ("我叫{0},我今年{1}岁了,我是{2}生,我可以吃喝拉撒睡呦~~", this.Name,this.Age,this.Gender); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 面向对象 { class Program { static void Main(string[] args) { //创建person类的对象 person Lucy = new person(); Lucy.Name = "露西"; Lucy.Age = -23; Lucy.Gender = '春'; Lucy.CHLSS(); Console.ReadLine(); } } }