1. 属性开头字母大写,字段揩油字母小写
calss Student
{
private int sex;
public int Sex
{
get
{
return sex;
}
set
{
sex=value;
}
public void SayHello()
{
Console.WriteLine("学生的性别是{0}",Sex);
}
}
2.只用set或者只用get就可以定义只读或者只写属性;
3.可以为set,get设置访问级别;
4.(.Net3.x)简化set,get:public int Sex(get;set;} 。适用于set,get中没有特殊逻辑代码的情况;
5.允许外部访问的值一定要声明为属性。作者: 李拴同 时间: 2013-3-3 21:09
类的建立 格式 class 类名{} 而属性的建立是public int Sex{get;set;} 开头大写