zoukankan      html  css  js  c++  java
  • 分不清IEnumerable和IEnumerator

    首先看几个单词

    Enum 枚举类型

    IEnumerator 枚举器

    IEnumerable 可枚举

    从单词上大概可以看出IEnumerator是实际枚举器

     

    查看源码

    源码分析:

    1 IEnumerable接口仅定义了一个返回IEnumerator类型的GetEnumerator方法

    2 IEumerator接口定义了三个方法,Current获取当前的对象返回Object类型,MoveNext指针下移,Reset将指针重置

    3 IEnumerator才是真正的集合遍历器,为了实现集合对象的遍历,必须实现IEnumerable接口

     

    操作实例

    1 定义实体类 球类

     1 namespace IEnumerableTest
     2 {
     3     class Ball
     4     {
     5         //编号
     6         public int No { get; set; }
     7         //颜色
     8         public string Color { get; set; }
     9         //球值
    10         public string Data { get; set; }
    11     }
    12 }
    View Code

    2 定义BallEnum 继承IEnumerator,实现MoveNext,Reset,Current方法

     1 class BallEnum:IEnumerator
     2     {
     3         public Ball[] _ball;
     4         int position = -1;
     5         public BallEnum(Ball[] ball)
     6         {
     7              _ball=ball;
     8         }
     9         public bool MoveNext()
    10         {
    11             position++;
    12             return (position < _ball.Length);
    13         }
    14         public void Reset()
    15         {
    16             position = -1;
    17         }
    18         public object Current
    19         {
    20             get {
    21                 try
    22                 {
    23                     return _ball[position];
    24                 }
    25                 catch (IndexOutOfRangeException)
    26                 {
    27                     throw new InvalidOperationException();
    28                 }
    29             }
    30         }
    31         public void Dispose()
    32         {
    33             throw new NotImplementedException();
    34         }
    35     }
    View Code

    3 定义实体类 摇奖机 继承接口IEnumerable 实现GetEnumerator方法

     1 class LotteryMachine:IEnumerable
     2     {
     3         private Ball[] _balls;
     4         public LotteryMachine(Ball[]balls)
     5         {
     6             _balls = new Ball[balls.Length];
     7             for (int i = 0; i < balls.Length; i++)
     8             {
     9                 _balls[i] = balls[i];
    10             }
    11         }
    12         IEnumerator IEnumerable.GetEnumerator()
    13         {
    14             return (IEnumerator)new BallEnum(_balls);
    15         }
    16     }
    View Code

    4 调用

     1             //当前号码
     2             int No;
     3             //抽取次数
     4             int times = 5;
     5             StringBuilder sb = new StringBuilder();
     6             //添加球
     7             Ball[] balls = new Ball[10]{
     8                 new Ball() { No = 1, Color = "bule" ,Data="34"},
     9                 new Ball() { No = 2, Color = "red" ,Data="21"},
    10                 new Ball() { No = 3, Color = "black",Data="01" },
    11                 new Ball() { No = 4, Color = "green",Data="03" },
    12                 new Ball() { No = 5, Color = "yellow",Data="12" },
    13                 new Ball() { No = 6, Color = "pink",Data="18" },
    14                 new Ball() { No = 7, Color = "orange",Data="58" },
    15                 new Ball() { No = 8, Color = "gray",Data="90" },
    16                 new Ball() { No = 9, Color = "white",Data="45" },
    17                 new Ball() { No = 10, Color = "purples",Data="00" },
    18             };
    19             //摇奖机,并把球放进去
    20             LotteryMachine lotteryMachine = new LotteryMachine(balls);
    21             //动起来
    22             Random r = new Random();
    23             for (int i = 0; i < times; i++)
    24             {
    25                 No = r.Next(1, balls.Length + 1);
    26                 foreach (Ball ball in lotteryMachine)
    27                 {
    28                     if (ball.No==No)
    29                     {
    30                         sb.Append(ball.Data);
    31                     }
    32                 }
    33                 if (i<times)
    34                 {
    35                     sb.Append(" ");
    36                 }
    37             }
    38             Console.WriteLine("本期中奖号码:");
    39             Console.WriteLine(sb.ToString());
    40             Console.ReadKey();
    41         
    View Code

    5 结果

  • 相关阅读:
    数往知来C#面向对象准备〈二〉
    数往知来C#之面向对象准备〈一〉
    如果我比别人走得更远些,那是因为我站在巨人的肩上。
    小记一下
    关于servlet
    使用session防止表单重复提交
    session和cookie的区别
    数据结构排序算法Java实现
    用背景渐变的透明度设置不同颜色的背景渐变
    Java用DOM方法解析xml
  • 原文地址:https://www.cnblogs.com/arvinzd/p/14157992.html
Copyright © 2011-2022 走看看