zoukankan      html  css  js  c++  java
  • IENumerable_Test

    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;
            }
        }
    }
    

      

  • 相关阅读:
    JD . 圆角矩形、权重层级、浮动撑开盒子及元素的默认间距、清除浮动、隐藏盒子、盒子的撑开与撑破、子盒子垂直居中|不占位置
    mac开发环境配置
    CSS常见兼容性问题总结
    初识Javascript.03 -- switch、自增、while循环、for、break、continue、数组、遍历数组、合并数组concat
    初识 Javascript.02 -- Date日期、Math对象、数据类型转换、字符串、布尔Boolean、逻辑运算符、if else 、三元表达式、代码调试方法、
    初识 Javascript.01 -- Javascript基础|输出方式、变量、变量命名规范、数据类型、
    CSS -- 练习之制作简单商品图
    CSS -- 练习(待续优化)
    javascript中作用域
    javascript中构造函数的三种方式
  • 原文地址:https://www.cnblogs.com/Jeely/p/11004510.html
Copyright © 2011-2022 走看看