【发布时间】:2017-03-11 05:22:25
【问题描述】:
/**
* Created by unibodydesignn on 11.03.2017.
*/
public interface Enumeration
{
// Returns true if another element in the collection exists
public boolean hasNext();
// Returns the next element in the collection as an Object
public Object getNext(); }
/**
* NameCollection implements a collection of names using
* a simple array.
*/
public class NameCollection
{
String[] names;
//this array will be initiliazed at outside
NameCollection(String[] names)
{
this.names = names;
}
/**
* getEnumeration should return an instance of a class that
implements
* the Enumeration interface where hasNext() and getNext()
* correspond to data stored within the names array.
*/
Enumeration getEnumeration ()
{
}
public boolean hasNext()
{
//i will define this method here
}
public Object getNext()
{
//i will define getNext() here
}
完成getEnumeration()方法,使其返回一个匿名内部类,对应于names数组的Enumeration接口 名称集合。然后编写一个创建 NamesCollection 的 main 方法 具有示例字符串数组的对象,通过以下方式检索此类的枚举 getEnumeration(),然后遍历枚举输出每个 使用 getNext() 方法命名。
我不明白这个问题的概念。我显然不知道该做什么或从哪里开始?我能找到 Java 的默认 hasNext() 定义吗? 这不是家庭作业。 它是 Absolute Java 书中的一个编程项目。第 13 章。P3。
【问题讨论】:
-
我怀疑这本书是凭空提出这样一个问题的。您是否阅读并理解了前面的章节? “Java 的默认 hasNext() 定义”是什么意思?为什么 Java 应该为您的自定义接口提供默认定义?
标签: java class interface inner-classes enumeration