JAVA IENumerablet
原创using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IENumerable_Test
{
public class Person
{
public string firstName;
public string lastName;
public Person(string fName, string lName)
{
this.firstName = fName;
this.lastName = lName;
}
}
public class People : IEnumerable
{
private Person[] _people;
public People(Person[] pArray)
{
this._people = pArray;
//_people = new Person[pArray.Length];
//for (int i = 0; i < pArray.Length; i++)
//{
// _people[i] = pArray[i];
//}
}
// Implementation for the GetEnumerator method.
IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator)GetEnumerator();
}
//IEnumerator IEnumerable.GetEnumerator()
// {
// throw new NotImplementedException();
// }
public PeopleEnum GetEnumerator()
{
return new PeopleEnum(_people);
}
}
public class PeopleEnum : IEnumerator
{
public Person[] _people;
// Enumerators are positioned before the first element
// until the first MoveNext() call.
int position = -1;
public PeopleEnum(Person[] list)
{
this._people = list;
}
public bool MoveNext()
{
position++;
return (position < _people.Length);
}
public void Reset()
{
position = -1;
}
object IEnumerator.Current
{
get
{
return Current;
}
}
public Person Current
{
get
{
try
{
return _people[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
}
class Program
{
static void Main(string[] args)
{
Person[] peopleArray = { new Person("John", "Smith"), new Person("Jim", "Johnson"), new Person("Sue", "Rabon") };
People peopleList = new People(peopleArray);
foreach (Person p in peopleList)
Console.WriteLine(p.firstName + " " + p.lastName);
Console.ReadKey();
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace test
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("*********Fun with IEnumberable/IEnumerator************
"); Garage carLot = new Garage();
//交出每一套每一套交出每一套Car对象吗
foreach (Car c in carLot)
{
Console.WriteLine("{0} is going {1} MPH", c.Name, c.Speed);
}
Console.WriteLine("GetEnumerator被定义为公共的,则对象用户可以与IEnumerator类型交互,以下结果与上面一致");
IEnumerator i = carLot.GetEnumerator();
while(i.MoveNext())
{
//i.current返回值类型是返回值类型是返回值类型是object的
Car myCar = (Car)i.Current;
Console.WriteLine("{0} is going {1} MPH", myCar.Name, myCar.Speed);
}
Console.ReadLine();
}
}
public class Garage : IEnumerable
{
Car[] carArray = new Car[4]; //在Garage在定义中定义在定义中定义Car类型的数组类型的数组类型的数组carArray,其实carArray这里的本质是一个数组字段,这里的本质是一个字段数组,这里本质上是一个数组字段
//在创业时填一些在创业时填一些在创业时填一些Car对象
public Garage()
{
//建立网络的能力--发展与各种人的联系和个人关系--是任何全球商业领袖的一项重要技能.唯一的问题是,当不同文化的网络规则有如此大的差异时,全球网络可能是非常困难的.事实上,这些文化挑战是如此强烈,以至于我认识的许多年轻的外国出生的潜在全球领导人常常有意避免在美国建立网络的机会--尽管这些机会对发展他们的事业是多么重要.
carArray[0] = new Car("Rusty", 30);
carArray[1] = new Car("Clunker", 50);
carArray[2] = new Car("Zippy", 30);
carArray[3] = new Car("Fred", 45);
}
public IEnumerator GetEnumerator()
{
//递归调用
return this.carArray.GetEnumerator();
}
}
public class Car
{
public string Name { get; set; }
public int Speed { get; set; }
public Car(string name, int speed)
{
this.Name = name;
this.Speed = speed;
}
}
}
转载于:https://www.cnblogs.com/Jeely/p/11004510.html
版权声明
所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除
上一篇:C语言可变参数实现 下一篇:ubuntu10.10构建内核树