.............................Windows Phone 7手机开发
浅谈类的字段,属性,关键字,修饰符,索引
举个例子更好说明:
public class User
{
#region
string userName;//这就像是字段
public string UserName//这就像是属性
{
get { return userName; }
set { userName = value; }
}
string userPwd; //这就像是字段
public string UserPwd//这就像是属性
{
get { return userPwd; }
set { userPwd = value; }
}
#endregion
public User(string UserName,string UserPwd)//这就是方法不过只是构造函数
{
this.UserName = UserName;
this.UserPwd = UserPwd;
}
}
字段:字段一般是被属性代替被访问。
属性:属性中有get和set 方法。Set是给字段赋值。Get是取字段的值
如果要只读的话,那么必须是get,去掉set
修饰符:public,private,protect,inneral等。
Public :是公有的,可以被子类使用,对象使用,程序外部使用,使用范围最广了。
Private:是私有的。只能是类内部使用的。其他任何都不能使用。
Protect:是保护类型,可以被类的内部使用,也可以被子类使用。但是不能被对象使用。
Inneral: 只能在程序集中使用。出了程序集就不能使用了。
Inneral : protect在一个程序中是凸显inneral,但是在另一个程序中使用的话是凸显protect
Out 和ref的使用:这两个都可以作为输出值。但是out 不用赋初值,ref需要赋初值。Out和ref都是像c语言一样给变量取个别名。加了out和ref 就像是引用类型了。
索引器:就是根据下标能够找到对应的值
举个例子更好理解
Class student
{
Float []scores=new float[10];
//使用索引器实现对某个数组和集合操作
Pubic float this[int index]//this代表的是索引器的名字
{
Get{return scores[index];}
Set{scores[index]=value;}//修改数组的某个元素
}
}
Student stu=new student();
Stu[0]=78;
Stu[1]=23;
这就是索引器的例子
.............................Windows Phone 7手机开发