接口实际上是一种标准,我们知道如果一个类继承于这个接口,那么就要给出接口里方法的实现,
实际上就是要求这个类要遵守这个标准。实际作用是,比如我们有某一个操作,我们想让这个操作对不同的类都能使用,
但各个类内部结构不同,要实现这个操作,可能会有细微的差别,所以我们通过让各个类继承于某一个接口,
也就找到了他们共同的遵守标准,然后我们就可以在这样的前提下,通过接口来操作各个类,来完成所说的那个操作了
1、索引符是一种特殊类型的属性,可以把它添加到一个类中,以提供类似数组的访问。实际上索引的实质就是利用list的可以按下标访问的特性,
比方:public class Animal:CollectionBase{ ……………… public Animal this[int animalIndex] { get {return (Animal)List[animalIndex];
//IList.List属性返回一个System.Object对象,所以要显式转换 } set { List[animalIndex] = value; } }}2、接口其实跟函数的重载差不多,
只不过被重载函数有实现,而接口没有实现,并且只能依赖类实现。实际上接口跟函数声明差不多,给出接口名和需要的形参,实际上的实现就可以由继承类自己写。
3、属性主要用于保护数据的安全,在C++中private数据只能通过公共函数操作,保护级别太高,有些时候不用这么复杂,因此在C#中推出了属性,
可以通过公共属性操作私有数据,通过设置其中的get和set可以使私有数据只读,或者可读可写
//存放Vector实例
private List<Vector> _vectors;
public List<Vector> Vectors
{
get
{
if (null == _vectors)
{
_vectors = new List<Vector>();
}
return _vectors;
}
}
/// <summary>
/// 返回或者设置第index条向量
/// </summary>
/// <param name="index">索引</param>
/// <returns></returns>
public Vector this[int index]
{
get
{
return _vectors[index];
}
set
{
_vectors[index] = value;
}
}