我的問題與我之前創建的稱爲FilteringFlatApplyFunction的類實現FlatApply類有關。我似乎無法正確執行應用程序,因爲靜態類繼續告訴我它需要抽象,而且@Override不按其應有的方式工作。我正在尋找的最終目標是使用繼承從FlatApply類中借用大部分功能並實現過濾器類的一種方法。我嘗試了許多不同的東西,但仍然無法得到它,謂詞「pred」檢查給定的謂詞是否爲真,如果是,則表示返回元素,我認爲這會實現FlatApply,我得到這個錯誤是什麼現在似乎永遠。感謝如何實現擴展應用方法? Java
錯誤:
FilteringFlatApplyFunction不是抽象的,不重寫抽象方法應用(T)在FlatApplyFunction 其中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在
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我也加了申請 –