首页 技术 正文
技术 2022年11月23日
0 收藏 813 点赞 4,609 浏览 1692 个字

public class Person:IEnumerable     //定义一个person类  并且 实现IEnumerable 接口  (或者不用实现此接口 直接在类 //里面写个GetEnumerator()方法)
    {
        string[] names = { “小杨”, “科比布莱恩特”, “凯文杜兰特”, “卡门安东尼” };
//在Person类里面定义一个字符串数组,以便用来模仿对象的索引访问

public int Count { get { return names.Length; } } 
  //可以通过对象访问此属性

public string this[int index]    //定义一个索引器
        {
            get { return names[index]; }
        }

public IEnumerator GetEnumerator()
        {
            return new MyClass(names);             //实际上通过此方法就是返回一个可以实现循环的类的对象  
                                                   // 用他的对象来移动索引
        }
    }

public class MyClass :IEnumerator
      {
          public MyClass(string[] names)                //一个参数的构造函数,用来和要遍历的类的进行关联
          {
              name = names;
          }
          private string[] name;                        //用此字段来存放接收过来的数组
          int index = -1;
          public object Current                        //获取当前索引的元素的值
          {
              get 
              {
                  if (index<0)                        //准备状态是-1,开始循环了在MoveNext中加1
                  {
                      return null;
                  }
                  else
                  {
                      return name[index];
                  }
              }
          }

public bool MoveNext()
          {
              ++index;                                      //每调用此方法就将索引往下+1
              if (index<name.Length)
              {
                  return true;
              }
              else
              {
                  return false;
              }
          }

public void Reset()
          {
             index=-1;
          }

}

在主方法里面:

class Program
    {
        static void Main(string[] args)
        {
            Person p = new Person();
            //for (int i = 0; i < p.Count; i++)
            //{
            //    Console.WriteLine(p[i]);
            //}
            foreach (string item in p)
            {
                Console.WriteLine(item);
            }
            //实际执行foreach就相当于执行下面几句话:
            Console.WriteLine(“==================================================”);
            IEnumerator p1 = p.GetEnumerator();
            while (p1.MoveNext())
            {
                string str=(string)p1.Current;
                Console.WriteLine(str);
            }
            Console.ReadKey();
        }
    }

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,918
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,444
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,255
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,069
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,702
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,741