1、使用属性封装方法
AccessModifer Type PropertyName { get{...} set{...} }
public int { get{ return this.X} set{this.x=rangeCheckX(value)} }
2、属性对某个字段封装时,命名规则,首字母大写
private int employeeID; public int EmployeeID { get{return this.EmployeeID;}; set{this.EmployeeID=value} }
3、接口中定义属性
interface IScreenPosition { int X{get;set;} int Y{get;set;} }
4、实例化同时赋值
static void DoWork() { Polygon square = new Polygon(); Polygon triangle = new Polygon { NumSides = 3 }; Polygon pentagon = new Polygon { SideLength = 15.5, NumSides = 5 }; Console.WriteLine("Square: number of sides is {0}, length of each side is {1}", square.NumSides, square.SideLength); Console.WriteLine("Triangle: number of sides is {0}, length of each side is {1}", triangle.NumSides, triangle.SideLength); Console.WriteLine("Pentagon: number of sides is {0}, length of each side is {1}", pentagon.NumSides, pentagon.SideLength); }