Создание пользовательского нумератора (интерфейсы lEnumerable и lEnumerator)

  • Published on
    24-Feb-2016

  • View
    35

  • Download
    0

Embed Size (px)

DESCRIPTION

( lEnumerable lEnumerator). Cars, r Cars: // Cars - r public class Cars { - PowerPoint PPT Presentation

Transcript

  • ( lEnumerable lEnumerator)

  • Cars, r Cars:// Cars - rpublic class Cars{private Car[] carArray;// Cars public Cars(){carArray = new Car[4];carArray[0] - new Car(FeeFee, 200, 0);carArray[l] = new Car(Clunker, 90. 0);carArray[2] - new Car(Zippy, 30. 0);carArray[l] - new Car(Fred, 30, 0);}}

  • // public class CarDriverpublic static void Main(){Cars carLot = new Cars();// foreach // , carLotforeach(Car in carlot){{Console.WriteLinet("Name: {0}, c.PetName);Console.WrlteLlne("Max speed: {0}, c.MaxSpeed);}}}

  • // foreach , I Enumerablepublic class Cars : lEnumerable{// lEnumerable ( ):public IEnumerator GetEnumerator(){// - ?}

  • // GetEnumerator() - public interface lEnumeratorbool MoveNext(); // ()// object Current {get:} // ( // )void Reset(); // ( // )}, lEnumerable.GetEnumerator() lEnumerator, Cars :// public class Cars: lEnumerable. lEnumerator{// lEnumerablepublic lEnumerator GetEnumerator(){return (lEnumerator)this;}

  • // !public class Cars: Enumerator, Enumerable{private car[] carArray:// int pos = -1;public Cars (){// }// IEnumeratorpublic bool MoveNext(){if (pos < carArray.Length>{pos++;return true:}elsereturn false:}public void Reset (){ pos = 0: }public object Current{get { return carArray[pos] ; }// lEnumerablepublic Enumerator GetEnumerator[]{return (lEnumerator)this:}}

  • -, foreach:// !foreach {Console.WriteLine("Name: {0}", c.PetName);Console.WriteLine("Max speed: {0}, c.MaxSpeed):}-, , Cars ( lEnumXXXX ):// IEnumeratorlEnumerator itfEnum;itfEnum = (lEnumerator)carLot;// itfEnum.Reset();// ItfEnum.MoveNext();// object curCar = itfEnum.Current;(()ur).CrankTunes(true):

Recommended

View more >