迭代器模式

public class MList: IIterable
{
    private static string[] _names = { "张三", "李四" };

    private string[] GetNames()
    {
        return _names;
    }

    public IIterator GetIterator(){
        return new MIterator();
    }


    private class MIterator : IIterator
    {
        private int _index;
        public bool HasNext()
        {
            return _index < _names.Length;
        }
        public string Next()
        {
            if (HasNext())
            {
                return _names[_index++];
            }
            return "";
        }

    }
}
public interface IIterator{
    bool HasNext();
    string Next();
}

 

public interface IIterable{
    IIterator GetIterator();
}

 

posted @ 2023-05-14 16:20  vba是最好的语言  阅读(11)  评论(0编辑  收藏  举报