私の質問は、このクラスの前にFilteringFlatApplyFunctionという名前で作成したFlatApplyクラスを実装することと関係しています。静的クラスが引き続き抽象的である必要があり、@Overrideが想定されている方法で動作していないと言い続けているため、適用を正しく実装できないようです。私が探している最終目標は、継承を使用してFlatApplyクラスからほとんどの機能を借用し、フィルタークラスを実装する方法です。私は多くの異なることを試みましたが、それでもそれを得ることはできません。述語predは、与えられた述語が真であるかどうかをチェックし、そうであれば要素を返すことを示します。今は永遠のようだ。おかげ拡張適用メソッドの実装方法は? Java
エラー:
FilteringFlatApplyFunctionが抽象的ではなく、抽象メソッドは、Tは、型変数ですFlatApplyFunction に(T)を適用上書きしません: TがクラスFilteringFlatApplyFunction
package iterators;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import iterators.Apply;
// Iterator that uses a Predicate to filter out elements from the input
public class Filter<T> extends FlatApply<T,T> {
public Filter(Predicate<T> p, Iterator<T> input) {
super(new FilteringFlatApplyFunction<T>(p),input);
}
// uses a Predicate to decide whether the input element is output or not
private static class FilteringFlatApplyFunction<T> implements FlatApplyFunction<T,T> {
private final Predicate pred;
public FilteringFlatApplyFunction(Predicate<T> p) {
this.pred = p;
}
@Override
public T apply(Iterator T) {
T result = null;
if((!T.hasNext())) throw new IllegalStateException();
if (pred.check(T.next()) == true){
result = (T) T.next();
}
else{
return (T) T;
}
}
で宣言されたオブジェクトを拡張
こちらはFlatApplyです
package iterators;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class FlatApply<InT,OutT> implements Iterator<OutT> {
private final Iterator<InT> input;
private final FlatApplyFunction<InT,OutT> f;
private final Queue<OutT> q;
public FlatApply(FlatApplyFunction<InT,OutT> f, Iterator<InT> input) {
this.input = input;
this.f = f;
this.q = new LinkedList<OutT>();
}
@Override
public boolean hasNext() {
if (!q.isEmpty()) {
return true;
}
else {
while(q.isEmpty() && input.hasNext()) {
List<OutT> result = f.apply(input.next());
q.addAll(result);
}
if(q.isEmpty()) return false;
else return true;
}
}
@Override
public OutT next() {
if((!hasNext())) throw new IllegalStateException();
return q.poll();
}
}
ここはFlatApplyFunctiです
public List<OutT> apply(InT x);
しかし、そのインターフェイスのFilteringFlatApplyFunction
実装は、これは言う::
package iterators;
import java.util.List;
public interface FlatApplyFunction<InT, OutT> {
public List<OutT> apply(InT x);
}
にここで適用されたクラスがある
package iterators;
import java.util.Iterator;
public class Apply<InT,OutT> implements Iterator<OutT> {
// The function that will be applied to each input element to make an output element
private final ApplyFunction<InT,OutT> f;
// The Iterator that this Apply object will get its input from
private final Iterator<InT> input;
public Apply(ApplyFunction<InT, OutT> f, Iterator<InT> input) {
this.input = input;
this.f = f;
}
@Override
public boolean hasNext() {
return input.hasNext();
}
@Override
public OutT next() {
if((!hasNext())) throw new IllegalStateException();
OutT result = f.apply(input.next());
return result;
}
}
「FlatApply」と「FlatApplyFunction」を表示してください。 – Jeremy
これは両方とも –
@Jeremyを表示するように更新されました。 –