[英]Return type interface for the method implemented, Ienumerator
the GetEnumerator return type is the interface itself ie Ienumerable, wondering what it means when an interface can be a method's return type; GetEnumerator返回类型是接口本身,即Ienumerable,想知道什么时候接口可以是方法的返回类型? specially when its defined in that interface. 特别是当它在该接口中定义时。
Ienumerator enum = arrayList.GetEnumerator();
If I understood you correctly. 如果我理解正确的话。 ArrayList doesn't implement IEnumerator , rather IEnumerable . ArrayList没有实现IEnumerator ,而是IEnumerable 。
public class ArrayList : IList, ICollection, IEnumerable, ICloneable
IEnumerable IEnumerable的
public interface IEnumerable
{
IEnumerator GetEnumerator();
}
Edit: 编辑:
IEnumerator has one simple responsibility. IEnumerator的职责很简单。
Supports a simple iteration over a non-generic collection. 支持对非泛型集合的简单迭代。
ArrayList's responsibility is different ArrayList的责任不同
Implements the IList interface using an array whose size is dynamically increased as required. 使用数组实现IList接口,该数组的大小根据需要动态增加。
May be this is required for maintaining of SRP. 可能是维护SRP所必需的。
ArrayList's implementation of GetEnumerator() probably returns implementation of IEnumerable which is aware of implementation details of ArrayList. ArrayList的GetEnumerator()实现可能返回IEnumerable的实现,该实现知道ArrayList的实现细节。 But concrete type is hidden from the client behind the IEnumerator interface. 但是具体类型在IEnumerator接口后面的客户端中是隐藏的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.