java enumeration接口_java Enumeration接口的问题

博客展示了一段Java代码,使用Vector类和Enumeration接口实现输入数字求和。作者对代码中‘Enumeration e=v.elements();’存在疑问,不理解e与Enumeration的关系,也不清楚Enumeration接口中hasMoreElements和nextElement方法是如何实现的。

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...那不是又返回了一个接口了- -??我是不明白谁实现了那两个方法...

展开

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值