0
私は本からいくつかのエクササイズを行い、次の演習でいくつか問題があります。パラメータ化されたスーパークラスに基づくジェネレータの作成にコンストラクタを使用
package net.mindview.util;
public interface Generator<T> { T next(); } ///:~
package net.mindview.util;
public class BasicGenerator1<T> implements Generator<T> {
private Class<T> type;
public BasicGenerator1(Class<T> type){ this.type = type; }
public T next() {
try {
// Assumes type is a public class:
return type.newInstance();
} catch(Exception e) {
throw new RuntimeException(e);
}
}
// Produce a Default generator given a type token:
public static <T> Generator<T> create(Class<T> type) {
return new BasicGenerator1<T>(type);
}
} ///:~
package cont;
public class CountedObject {
private static long counter = 0;
private final long id = counter++;
public long id() { return id; }
public String toString() { return "CountedObject " + id;}
} ///:~
package cont;
import net.mindview.util.*;
public class BasicGeneratorDemo {
public static void main(String[] args) {
//this line works
//Generator<CountedObject> gen = BasicGenerator1.create(CountedObject.class);
//am stuck here
Generator<CountedObject> gen = new BasicGenerator1<CountedObject>;
for(int i = 0; i < 5; i++)
System.out.println(gen.next());
}
}
一般的なcreate()メソッドの代わりに明示的なコンストラクタを使用するように、BasicGeneratorDemo.javaを変更する必要があります。それをどうすれば実現できますか?
はあなたに
あなたはBasicGeneratorDemoを追加し、あなたが立ち往生しているところ私たちを示してもらえますか? –
申し訳ありません、間違ったクラスがコピーされました。今は大丈夫だし、コメントがついた場所を指しています。 – aretai