标签:
1 System.Array类和System.collections.ArrayList类
示例:控制台程序,新疆三个类,抽象类Animal以及两个继承类Cows和Chicken
Animal.cs
public abstract class Animal
{
protected string name;
public string Name
{
get { return name; }
set { name = value; }
}
public Animal()
{
name = "The animal with no name";
}
public Animal(string newName)
{
name = newName;
}
public void Feed()
{
Console.WriteLine("{0} has been fed.", name);
}
}
Cows.cs
public class Cow : Animal
{
public void Milk()
{
Console.WriteLine("{0} has been fed.",name);
}
public Cow(string newName)
: base(newName)
{
}
}
Chicken.cs
public class Chicken : Animal
{
public void LayEgg()
{
Console.WriteLine("{0} has been fed.", name);
}
public Chicken(string newName)
: base(newName)
{
}
}
Program.cs
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Create an Array type collection of Animal " + "object and use it");
Animal[] animalArray = new Animal[2];
Cow myCow1 = new Cow("Deirdre");
animalArray[0] = myCow1;
animalArray[1] = new Chicken("Ken");
foreach (Animal myAnimal in animalArray)
{
Console.WriteLine("New {0} object added to Array collection," + "Name = {1}", myAnimal.ToString(), myAnimal.Name);
}
Console.WriteLine("Array collection contatains {0} objects.",animalArray.Length);
animalArray[0].Feed();
((Chicken)animalArray[1]).LayEgg();
Console.WriteLine();
Console.WriteLine("Create an ArrayList type collection of Animal " + "object and use it");
ArrayList animalArrayList = new ArrayList();
Cow myCow2 = new Cow("Hayley");
animalArrayList.Add(myCow2);
animalArrayList.Add(new Chicken("Roy"));
foreach(Animal myAnimal in animalArrayList)
{
Console.WriteLine("New {0} object added to ArrayList collection," + "Name = {1}",myAnimal.ToString(),myAnimal.Name);
}
Console.WriteLine("ArrayList collection contains {0} objects.",animalArrayList.Count);
((Animal)animalArrayList[0]).Feed();
((Chicken)animalArrayList[1]).Feed();
Console.WriteLine();
Console.WriteLine("Additional manipulaion of ArrayList:");
animalArrayList.RemoveAt(0);
((Animal)animalArrayList[0]).Feed();
animalArrayList.AddRange(animalArray);
((Chicken)animalArrayList[2]).LayEgg();
Console.WriteLine("The animal called {0} is at index {1}.",myCow1.Name,animalArrayList.IndexOf(myCow1));
myCow1.Name = "Janice";
Console.WriteLine("The animal is called {0}.",((Animal)animalArrayList[1]).Name);
Console.ReadKey();
}
}
输出结果:
这个示例创建了两个对象集合,第一个使用System.Array类(这是一个简单的数组),第二个集合使用了System.collections.ArrayList类。
对于简单的数组来说,只有用固定的大小初始化后,才能使用它。
Animal[] animlaArray = new Animal[2];
而ArrayList集合不需要初始化其大小,所以可以使用一下代码创建列表animalArrayList;
ArrayList[] myArrayList = new ArrayList();
因为数组是引用类型,因此用一个长度初始化数组并没有初始化他所包含的项。要使用一个指定的项,该项还需要初始化,即需要给这个项赋予初始化了的对象:
Cow myCow1 = new Cow("Deirdre");
animalArray[0] = myCow1;
animalArray[1] = new Chicken("Ken");
对于ArrayList集合,他没有现成的项。他没有null引用的项。这样就不能以相同的方式给索引赋予新实例。我们是用ArrayList对象的add()方法加新项:
Cow myCow2 = new Cow("hayley");
animalArrayList.Add(myCow2);
animalArrayList.Add(new Chicken("Roy"));
注意:
数组的类型是抽象类型Animal,因此不能直接调用由派生类提供的方法,而必须使用数据类型转换:
((Chicken)animalArray[1]).LayEgg();
2 定义集合,创建自己的、强化类型的集合
2.1 System.Collections.CollectionBase类
实例:
在上面的基础之上添加一个新类Animals.cs
Animals.cs
public class Animals : CollectionBase
{
public void Add(Animal newAnimal)
{
List.Add(newAnimal);
}
public void Remove(Animal newAnimal)
{
List.Remove(newAnimal);
}
public Animals()
{
Console.WriteLine("This is Animals!!!");
}
public Animal this[int animalIndex]
{
get
{
return (Animal)List[animalIndex];
}
set
{
List[animalIndex] = value;
}
}
}
Program.cs
class Program
{
static void Main(string[] args)
{
Animals animalCollection = new Animals();
animalCollection.Add(new Cow("Jack"));
animalCollection.Add(new Chicken("Vera"));
foreach (Animal myAnimal in animalCollection)
{
myAnimal.Feed();
}
Console.ReadKey();
}
}
输出结果:
这个实例使用了上一节详细介绍的代码,实现类Animal中强化类型的Animal对象集合。Main()中代码仅实例化了一个Animals对象animalCollection,添加了两个项,再使用foreach循环调用这个两个对象继承于基类Animal的Feed方法。
3 迭代器
迭代器的定义是,他是一个代码块,按顺序提供了要在foreach循环中使用的所有值。一般情况下,这个代码块是一个方法,但也可以使用访问器和其他代码块作为迭代器。这里为了简单起见,介绍方法:
public static IEnumerable SimpleList()
{
yield return "string 1";
yield return "string 2";
yield return "string 3";
}
public static void Main(string[] args)
{
foreach(string item in SimpleList())
{
Console.WriteLine(item);
}
Console.ReadKey;
}
实际上,并没有返回string类型的项,而是返回了object类型的值。因为object是所有类型的基类,也就是说,可以从yield语句中返回任意类型。
标签:
原文地址:http://www.cnblogs.com/DannyShi/p/4501594.html