【发布时间】:2016-01-24 22:44:36
【问题描述】:
我正在尝试找出教科书中问题的答案,但遇到了麻烦。问题是要求我检查一个输入数组以查看它是否包含适合某些功能的对象。除了我尝试使用作为实现接口的类之一的输入来实现该方法之外,此功能的每个功能似乎都运行良好。
例子:
main(){
boolean r1 = has(input, checkFor2);
}
public static <T> boolean has(T[] input, Check<T> c){
// does its check algorithm and returns a boolean
}
static public interface Check<T>{
boolean isContained(T item);
}
static public class checkFor2 implements Check<Integer>{
public boolean isContained(Integer val){
// does a check algorithm
}
}
// Other check algorithms implementing Check<T> follow as well.
我得到的错误是在 main 方法中调用“has”。它说:
main类型中的方法
has(T[], main.Check<T>)不适用于参数(int[], main.checkFor2)
它的建议是将方法中的类更改为特定的类而不是接口,这违背了以这种方式编写它的目的。
我在编写这个代码时犯了一些新手错误吗?
【问题讨论】: