importjava.util.*;//下面用到的Vector类和Enumeration接口都在此包中publicclassTestVector{publicstaticvoidmain(String[]args){intb=0;Vectorv=newVector();System.out.println("P...
import java.util.*; //下面用到的Vector类和Enumeration接口都在此包中
public class TestVector
{
public static void main(String [] args)
{
int b=0;
Vector v=new Vector();
System.out.println("Please Enter Number:");
while(true)
{
try
{
b= System.in.read();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
if(b=='\r' || b== '\n')
break;
else
{
int num=b-'0';
v.addElement(new Integer(num));
}
}
int sum=0;
Enumeration e=v.elements();
while(e.hasMoreElements())
{
Integer intObj=(Integer)e.nextElement();
sum += intObj.intValue();
}
System.out.println(sum);
}
}
以上也算一段古老的代码了(很多论坛看到),但是翻阅了大半天还是有点东西没弄懂:
Enumeration e=v.elements();
这句我是不是应该理解成e是一个实现Enumeration接口的类,但是返回给e的类应该是Integer对象啊,我在它的源码还有它的父类Number都没看到有覆盖了Enumeration接口中的hasMoreElement跟nextElement的方法啊?这两个方法究竟是怎么实现的啊?
其实我最想知道的是e跟Enumeration究竟是什么关系啊?其他的我都明白,就是这句想不透。
如果返回的是Enumertation...那不是又返回了一个接口了- -??我是不明白谁实现了那两个方法...
展开
博客展示了一段Java代码,使用Vector类和Enumeration接口实现输入数字求和。作者对代码中‘Enumeration e=v.elements();’存在疑问,不理解e与Enumeration的关系,也不清楚Enumeration接口中hasMoreElements和nextElement方法是如何实现的。

6228

被折叠的 条评论
为什么被折叠?



