C#核心知识回顾——16.迭代器

using System.Collections;
using UnityEngine;

public class test : MonoBehaviour
{
    #region 知识点一选代器是什么
    //选代器(iterator)有时又称光标(cursor)
    //是程序设计的软件设计模式
    //选代器模式提供一个方法顺序访问一个聚合对象中的各个元素
    //而又不暴露其内部的标识
    //在表现效果上看
    //是可以在容器对象(例如链表或数组)上遍历访问的接口
    //设计人员无需关心容器对象的内存分配的实现细节
    //可以用for each遍历的类,都是实现了选代器的
    #endregion

    # region 知识点2标准选代器的实现方法
    //关键接口:IEnumerator,IEnumerable
    //命名空间:using System.Collections;
    //可以通过同时继承IEnumerable和IEnumerator实现其中的方法

    class CustomList : IEnumerable,IEnumerator
    {
        private int[] list;
        private int position = -1;//光标,用于遍历
        public CustomList()
        {
            list = new int[] {1,2,3,4,5,6};
        }
        #region IEnumerator
        public object Current
        {
            get
            {
                return list[position];
            }
        }
        public bool MoveNext()
        {
            //移动光标
            position++;
            //溢出则不合法
            return position < list.Length;
        }

        public void Reset()
        {
            position = -1;
        }
        #endregion
        #region IEnumerable
        public IEnumerator GetEnumerator()
        {
            Reset();
            return this;
        }        
        #endregion

    }
    #endregion

    #region 知识点三用yield return语法糖实现选代器
    //yield return是c#提供给我们的语法糖
    //所谓语法糖,也称糖衣语法
    //主要作用就是将复杂逻辑简单化,可以增加程序的可读性
    //从而减少程序代码出错的机会
    //关键接口:IEnumerable
    //命名空间:using System.Collections;
    //让想要通过foreach遍历的自定义类实现接口中的方法GetEnumerator即可
    class CustomList2 : IEnumerable
    {
        private int[] list;
        public CustomList2()
        {
            list = new int[] { 1, 2, 4, 5, 6 };
        }
        public IEnumerator GetEnumerator()
        {
            for (int i = 0; i < list.Length; i++)
            {
                yield return list[i];
            }
        }
    }
    #endregion

    #region 知识点四用yield return语法糖为泛型类实现选代器

    class CustomList3<T>:IEnumerable
    {
        private T[] array;
        public CustomList3(params T[] array)
        {
            this.array = array;
        }
        public IEnumerator GetEnumerator()
        {
            for (int i = 0; i < array.Length; i++)
            {
                yield return array[i];
            }                
        }
    }

#endregion

    private void Start()
    {
        CustomList list2 = new CustomList();
        //本质:
        //1.获取in 后面的IEnumetor,调用其中的GetEnumerator获取
        //2.执行得到这个IEnumerator对象中的 MoveNext方法
        //3.只要MoveNext方法的返回值时true就会去得到current 然后复制给item       
        foreach (int item in list2)
        {
            Debug.Log(item);
        }
        CustomList2 list3 = new CustomList2();
        foreach (int item in list3)
        {
            Debug.Log(item);
        }
        CustomList3<string> list4 = new CustomList3<string>("1", "2", "3", "4", "5");
        foreach (string item in list4)
        {
            Debug.Log(item);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_29296473/article/details/132027042
今日推荐