gpt4 book ai didi

java - 如何实现扩展apply方法? java

转载 作者:行者123 更新时间:2023-12-02 13:42:51 26 4
gpt4 key购买 nike

我的问题与实现我之前在名为 FilteringFlatApplyFunction 的类中创建的 FlatApply 类有关。我似乎无法正确实现 apply,因为静态类继续告诉我它需要是抽象的,并且 @Override 没有按预期的方式工作。我正在寻找的最终目标是一种使用继承来借用 FlatApply 类的大部分功能并实现过滤器类的方法。我尝试了很多不同的事情,但仍然无法得到它,谓词“pred”检查给定的谓词是否为真,如果是,则指示返回元素,我认为这会实现 FlatApply,我一直收到此错误现在看来就像是永远。谢谢

错误:

FilteringFlatApplyFunction 不是抽象的,不会覆盖 FlatApplyFunction 中的抽象方法 apply(T) 其中 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();
}


}

这是 FlatApplyFunction

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;
}
}

最佳答案

FlatApplyFunction 接口(interface)是这样说的:

public List<OutT> apply(InT x);

但是该接口(interface)的 FilteringFlatApplyFunction 实现是这样说的:

public T apply(Iterator T) {

该接口(interface)需要返回一个List,而您只需返回T。此外,参数必须是 T,但您将其作为 Iterator

使它们匹配,这个编译器错误就会消失。

关于java - 如何实现扩展apply方法? java ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42639881/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com