属性的作用就是保护字段、对字段的赋值和取值进行限定。
属性的本质就是两个方法,一个叫set(),一个叫get().  //可读可写属性
Field 字段
Method 方法
Property 属性
访问修饰符:
public**:公开的公共的,在哪都能访问。
private**:私有的,只能在当前类的内部进行访问。
**
using System;using System.Collections.Generic;using System.Text;namespace _048_面对对象初级_3_属性{class Person{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 != '女'){_gender = '男';//return _gender = '男';}return _gender;}set { _gender = value; }}public void Habit(){Console.WriteLine("我叫{0},是{1}生,今年{2}岁了,喜欢打lol看动漫欣赏妹子。", this.Name, this.Gender, this.Age );}}}
using System;namespace _048_面对对象初级_3_属性{class Program{static void Main(string[] args){Person person; //不占内存//创建person类的对象Person wxl = new Person();//占内存//其他类中,字段被private修饰后,就访问不到了//wxl._name = "微咲";//wxl._age = 19;//wxl._gender = '男';wxl.Name = "微咲";wxl.Age = -19;wxl.Gender = '人';wxl.Habit();Console.ReadKey();}}}
