[英]returning inner-class object from interface
/**
* 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(),以便它返回一個匿名內部類,該內部類對應於NamesCollection中names數組的Enumeration接口。 然后編寫一個main方法,使用示例字符串數組創建NamesCollection對象,通過getEnumeration()檢索此類的Enumeration,然后使用getNext()方法迭代輸出每個名稱的枚舉。
我不明白這個問題的概念。 我顯然不知道該做什么或從哪里開始? 我可以找到Java的默認hasNext()定義嗎? 這不是功課。 它是Absolute Java書籍的編程項目。 第13章P3。
完成方法
getEnumeration()
以便它返回一個匿名內部類,該內部類對應於NamesCollection
names數組的Enumeration
接口。
練習的目的似乎是與匿名課程一起工作。 例如,而不是像這樣創建一個命名類:
class NamesEnumeration implements Enumeration {
@Override
public boolean hasNext() {
// ...
}
@Override
public Object getNext() {
// ...
}
}
...說明指導您使用匿名類,如下所示:
Enumeration getEnumeration() {
return new Enumeration() {
@Override
public boolean hasNext() {
// ...
}
@Override
public Object getNext() {
// ...
}
};
}
重要的一點是,匿名實現可以使用其范圍內可見的變量。 最值得注意的是,此示例包含了NamesCollection
類的names
字段。
在NamesCollection
類中,您不需要hasNext
和getNext
方法。 所以這個類應該是這樣的:
public class NameCollection {
final String[] names;
NameCollection(String[] names) {
this.names = names.clone();
}
Enumeration getEnumeration() {
return new Enumeration() {
int currentIndex = 0;
// ^^^^^^^^^^^^ this is a hint for you
@Override
public boolean hasNext() {
// ...
}
@Override
public Object getNext() {
// ...
}
};
}
}
我做了一些小的改進,並添加了一個提示,以幫助您完成實施。
最后,練習還要求添加一個main
方法來練習這個課程。 那應該是這樣的:
public static void main(String[] args) {
String[] sample = {"hello", "world"};
NameCollection namesCollection = new NameCollection(sample);
Enumeration names = namesCollection.getEnumeration();
while (names.hasNext()) {
System.out.println(names.getNext());
}
}
我不知道你說的那本書,但讓我們了解所要求的內容:
你需要做的是創建Enumeration Interface
的實現,我不知道這一章是關於Interfaces
還是Enumarations
。
1: “完成方法getEnumeration(),以便它返回一個匿名內部類,該類對應於NamesCollection中的names數組的Enumeration接口” 。
在這里你需要返回一個Enumeration
接口的實現,問題就是創建一個Anonymous類(但我想創建一個內部類,也許是私有內部類)。 像這樣,在NameCollection
類中:
public Enumeration getEnumeration(){
Enumeration enumerat = new Enumeration(){
private int index = 0;
public boolean hasNext(){
return names.length > index;
}
public Object getNext(){
return names[index++];
}
};
return enumerat;
}
該方法返回Enumeration
類的實現,您可以使用該實現遍歷傳遞給NameCollection
類的構造函數的名稱數組。
2: “然后編寫一個main方法,使用示例字符串數組創建NamesCollection對象,通過getEnumeration()檢索此類的枚舉,然后使用getNext()方法迭代輸出每個名稱的枚舉” 。
在這里,您只需要為您的實現創建一個測試類:
public class Main {
public static void main(String[] args) {
NameCollection nc = new NameCollection(new String[]{ "Adriane", "Beatriz" });
Enumeration en = nc.getEnumeration();
while( en.hasNext() ){
System.out.printf("Name: %s \n", en.getNext() );
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.