1.控制字段的访问权限,只读(get),只写(set),可读可写(get,set)
public class Student
{
private string name
public string Name
{
get{return name;}//这里取出私有属性name的值
set{name=value;}//这里是给私有属性name赋值
}
}
2.在get,set中增加权限
using System;
class MyClass
{
private int id;
public int Id
{
get {return id;}
set
{
if(value<0){ value=0}
else{id=value}
}
}
}