首页 新闻 会员 周边 捐助

迭代得错误??

0
[已关闭问题]
<P>using System;<BR>using System.Collections;<BR>using System.Collections.Generic;<BR>public class Stack&lt;T&gt; : IEnumerable&lt;T&gt;<BR>{<BR>&nbsp;&nbsp;&nbsp; //数组,索引<BR>&nbsp;&nbsp;&nbsp; T[] items;<BR>&nbsp;&nbsp;&nbsp; int idx;<BR>&nbsp;&nbsp;&nbsp; public Stack()<BR>&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; idx = 0;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; items = new T[10];</P> <P>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; public void Push(T t)<BR>&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; items[idx++] = t;<BR>&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; public T Pop()<BR>&nbsp;&nbsp;&nbsp; { <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return items[--idx];<BR>&nbsp;&nbsp;&nbsp; }</P> <P><BR>&nbsp;&nbsp;&nbsp; public IEnumerator&lt;T&gt; GetEnumerator()<BR>&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = items.Length - 1; i &gt;= 0; --i)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; yield return items[i];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; }</P> <P>}<BR>class App<BR>{<BR>&nbsp;&nbsp;&nbsp; static void Main()<BR>&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Stack&lt;int&gt; stack = new Stack&lt;int&gt;();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stack.Push(100);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stack.Push(101);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stack.Push(103);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stack.Push(104);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stack.Push(105);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stack.Push(106);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stack.Push(107);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stack.Push(108);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stack.Push(109);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stack.Push(110);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stack.Push(111);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; stack.Push(112);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foreach (int i in stack)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(i.ToString());<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.Read();</P> <P>&nbsp;&nbsp;&nbsp; }<BR>}提示错误:<FONT face=Verdana>错误&nbsp;1&nbsp;“Stack&lt;T&gt;”不会实现接口成员“System.Collections.IEnumerable.GetEnumerator()”。“Stack&lt;T&gt;.GetEn
roboth的主页 roboth | 初学一级 | 园豆:28
提问于:2008-04-06 21:51
< >
分享
其他回答(1)
0
using System; using System.Collections; using System.Collections.Generic; public class Stack<T> : IEnumerable<T> { //数组,索引 T[] items; int idx; public Stack() { idx = 0; items = new T[10]; } public void Push(T t) { items[idx++] = t; } public T Pop() { return items[--idx]; } public IEnumerator<T> GetEnumerator() { for (int i = items.Length - 1; i >= 0; --i) { yield return items[i]; } } // ------- 加上这个方法就OK了 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { for (int i = items.Length - 1; i >= 0; --i) { yield return items[i]; } } //---------------- } class App { static void Main() { Stack<int> stack = new Stack<int>(); stack.Push(100); stack.Push(101); stack.Push(103); stack.Push(104); stack.Push(105); stack.Push(106); stack.Push(107); stack.Push(108); stack.Push(109); stack.Push(110); stack.Push(111); stack.Push(112); foreach (int i in stack) { Console.WriteLine(i.ToString()); } Console.Read(); } }
deerchao | 园豆:8367 (大侠五级) | 2008-04-07 02:21
0
请查看一下 IEnumerable<T> 的定义,如下 public interface IEnumerable<T> : IEnumerable IEnumerable<T> 继承于 IEnumerable,IEnumerable 接口包含一个GetEnumerator() 方法,IEnumerable<T> 也包含一个该方法,不同之处在于后者的返回值是泛型。 如果两个接口的方法同名,就用到了 “显式接口”,你的错误就在于少了一个实现IEnumerable 接口方法的显式实现。 至于怎么实现,请在MSDN内输入“显式接口”四个字,有详细说明。 楼上给出了实现。
杨连国 | 园豆:295 (菜鸟二级) | 2008-04-07 09:25
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册