C#中CollectionBase类中IList接口的实现
2008-01-21 11:14
211 查看
http://www.cnblogs.com/hg98/archive/2007/06/12/780950.html
今天看了一下C#中接口的东西,发现对CollectionBase(为强类型集合提供抽象基类)中的具体实现原理不是很了解。经过查看一些资料,分析过后得到以下的初步认识。
CollectionBase主要显示的实现ICollection和IList接口。具体的显示接口实现,可以查看MSDN的相关资料。(注: ICollection主要是实现将自己的项目复制到一个项目中,IList接口主要用于按照索引单独访问的一组对象)。由于IList接口成员很多,在 这里就用Add接口成员来描述,并给出一个《C#入门经典》的例子的阐述。
我们的目的是实现一个强类型集合,并且实现基本的添加,删除,和索引访问。这些实现都是通过继承CollectionBase类和使用IList接口来实现的。首先看CollectionBase的几个主要成员:
List成员定义: protected IList List {get;} //一个List属性
显示接口实现: int IList.Add( object value) //一个显示实现
我们通过List属性调用显示接口来实现元素的添加。同样删除和索引访问都要使用List属性成员来调用显示接口。
下面是代码和阐述:
//======================Animals.cs ============================
//强类型化定制集合类
//===========================================================
using System;
using System.Collections;
namespace Ch11Ex02
{
/**//// <summary>
/// Animals 的摘要说明。
/// </summary>
public class Animals:CollectionBase
{
public Animals()
{
}
public void Add(Animal newAnimal)
{
List.Add(newAnimal);//List是IList接口的一个引用变量,调用CollectionBase中的显示接口成员int IList.Add(),实现元素的添加。注: 因为int IList.Add()是显示接口成员,所以必须使用IList类型引用变量才能调用
}
public void Remove(Animal oldAnimal)
{
List.Remove(oldAnimal);//和Add一样,使用List引用变量调用void IList.Remove(object value)
}
public Animal this[int animalIndex] //类中的索引
{
get
{
return (Animal)List[animalIndex]; //返回对应索引值的Animal对象
}
set
{
List[animalIndex]=value;
}
}
}
}
//======================Animal.cs ============================
//Animal类,其对象作为Animals集合类的项目
//===========================================================
using System;
namespace Ch11Ex02
{
/**//// <summary>
/// Animal 的摘要说明。
/// </summary>
public abstract class Animal
{
protected string name;
public Animal()
{
name="The animal with no name";
}
public Animal(string newName)
{
name=newName;
}
public string Name
{
get
{
return name;
}
set
{
name=value;
}
}
public void Feed()
{
Console.WriteLine("{0} has been fed",name);
}
}
public class Cow:Animal
{
public void Milk()
{
Console.WriteLine("{0} has been milked",name);
}
public Cow(string newName):base(newName)
{
}
}
public class Chicken:Animal
{
public void LayEgg()
{
Console.WriteLine("{0} has laid an egg",name);
}
public Chicken(string newName):base(newName)
{
}
}
}
//======================Class1.cs ============================
//主类,一个应用程序
//===========================================================
using System;
namespace Ch11Ex02
{
/**//// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
Animals animalCollection=new Animals();
animalCollection.Add(new Cow("Jack"));
animalCollection.Add(new Chicken("Vera"));
foreach(Animal myAnimal in animalCollection)
{
myAnimal.Feed();
}
}
}
}
今天看了一下C#中接口的东西,发现对CollectionBase(为强类型集合提供抽象基类)中的具体实现原理不是很了解。经过查看一些资料,分析过后得到以下的初步认识。
CollectionBase主要显示的实现ICollection和IList接口。具体的显示接口实现,可以查看MSDN的相关资料。(注: ICollection主要是实现将自己的项目复制到一个项目中,IList接口主要用于按照索引单独访问的一组对象)。由于IList接口成员很多,在 这里就用Add接口成员来描述,并给出一个《C#入门经典》的例子的阐述。
我们的目的是实现一个强类型集合,并且实现基本的添加,删除,和索引访问。这些实现都是通过继承CollectionBase类和使用IList接口来实现的。首先看CollectionBase的几个主要成员:
List成员定义: protected IList List {get;} //一个List属性
显示接口实现: int IList.Add( object value) //一个显示实现
我们通过List属性调用显示接口来实现元素的添加。同样删除和索引访问都要使用List属性成员来调用显示接口。
下面是代码和阐述:
//======================Animals.cs ============================
//强类型化定制集合类
//===========================================================
using System;
using System.Collections;
namespace Ch11Ex02
{
/**//// <summary>
/// Animals 的摘要说明。
/// </summary>
public class Animals:CollectionBase
{
public Animals()
{
}
public void Add(Animal newAnimal)
{
List.Add(newAnimal);//List是IList接口的一个引用变量,调用CollectionBase中的显示接口成员int IList.Add(),实现元素的添加。注: 因为int IList.Add()是显示接口成员,所以必须使用IList类型引用变量才能调用
}
public void Remove(Animal oldAnimal)
{
List.Remove(oldAnimal);//和Add一样,使用List引用变量调用void IList.Remove(object value)
}
public Animal this[int animalIndex] //类中的索引
{
get
{
return (Animal)List[animalIndex]; //返回对应索引值的Animal对象
}
set
{
List[animalIndex]=value;
}
}
}
}
//======================Animal.cs ============================
//Animal类,其对象作为Animals集合类的项目
//===========================================================
using System;
namespace Ch11Ex02
{
/**//// <summary>
/// Animal 的摘要说明。
/// </summary>
public abstract class Animal
{
protected string name;
public Animal()
{
name="The animal with no name";
}
public Animal(string newName)
{
name=newName;
}
public string Name
{
get
{
return name;
}
set
{
name=value;
}
}
public void Feed()
{
Console.WriteLine("{0} has been fed",name);
}
}
public class Cow:Animal
{
public void Milk()
{
Console.WriteLine("{0} has been milked",name);
}
public Cow(string newName):base(newName)
{
}
}
public class Chicken:Animal
{
public void LayEgg()
{
Console.WriteLine("{0} has laid an egg",name);
}
public Chicken(string newName):base(newName)
{
}
}
}
//======================Class1.cs ============================
//主类,一个应用程序
//===========================================================
using System;
namespace Ch11Ex02
{
/**//// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
Animals animalCollection=new Animals();
animalCollection.Add(new Cow("Jack"));
animalCollection.Add(new Chicken("Vera"));
foreach(Animal myAnimal in animalCollection)
{
myAnimal.Feed();
}
}
}
}
相关文章推荐
- NET(C#)接入Dubbo服务,Zookeeper作为Dubbo服务的注册中心,实现thrift协议访问接口(2)
- C#中接口的实现方法
- C#开发微信门户及应用(28)--微信“摇一摇·周边”功能的使用和接口的实现
- C#实现QQ接口软件--QQ的HTTP接口协议探究
- 显式接口实现(C# 编程指南)
- 浅谈C# 多态的魅力(虚方法,抽象,接口实现)(转)
- C#中显/隐式实现接口及其访问方法
- NET(C#)接入Dubbo服务,Zookeeper作为Dubbo服务的注册中心,实现thrift协议访问接口(1)
- Java 调用C# webservice接口 生成java客户端 实现方式
- C# 动态实现接口
- 浅谈C# 多态的魅力(虚方法,抽象,接口实现)
- C#正确实现IDisposable接口
- 用JPUSH极光推送实现服务端向安装了APP应用的手机推送消息(C#服务端接口)
- 全面剖析C#接口编程之实现接口
- 支付宝Payto接口的c#.net实现
- C#区分接口实现与虚函数重载
- C#接口深度剖析(子类重新实现)
- C#中实现两接口中的同名方法
- C#中结构体实现接口后是值类型还是引用类型
- C#开发微信门户及应用(42)--使用Autofac实现微信接口处理的控制反转处理