gpt4 book ai didi

java - Java 中的方法重载 : The method is not applicable for arguments

转载 作者:行者123 更新时间:2023-11-29 07:12:46 25 4
gpt4 key购买 nike

我正在尝试使用谓词,但我不能,因为方法重载正在起作用...

我想对数组 (varargs) 使用 filter,并且我想在谓词中使用内置方法,过滤转换为列表的数组。

这是错误:类型 Predicates 中的方法 filter(Iterable, Predicate) 不适用于参数 (Class[], Predicate)

private static final Predicate<Method> isTestMethod = new Predicate<Method>() {
@Override
public boolean evaluate(Method input) {
return input.isAnnotationPresent(Test.class);
}
};

public static void testClasses(Class<?>... classes) {
for (Method method : filter(classes, isTestMethod)) {

}
}

这是谓词方法:

/**
* Returns the elements of <tt>unfiltered</tt> that satisfy a predicate.
*
* @param unfiltered An iterable containing objects of any type
* that will be filtered and used as the result.
* @param predicate The predicate to use for evaluation.
* @return An iterable containing all objects which passed the predicate's evaluation.
*/
public static <T> Iterable<T> filter(Iterable<T> unfiltered, Predicate<T> predicate) {
checkNotNull(unfiltered);
checkNotNull(predicate);

List<T> result = new ArrayList<T>();
Iterator<T> iterator = unfiltered.iterator();
while (iterator.hasNext()) {
T next = iterator.next();
if (predicate.evaluate(next)) {
result.add(next);
}
}
return result;
}

/**
* Returns the elements of <tt>unfiltered</tt> that satisfy a predicate.
*
* @param unfiltered An array containing objects of any type
* that will be filtered and used as the result.
* @param predicate The predicate to use for evaluation.
* @return An iterable containing all objects which passed the predicate's evaluation.
*/
public static <T> Iterable<T> filter(T[] unfiltered, Predicate<T> predicate) {
return filter(Arrays.asList(unfiltered), predicate);
}

最佳答案

您的过滤器适用于方法 - 但您有一组。您不能将您的 isTestMethod 谓词应用于类...

您预计它会做什么?您是否正在寻找一个过滤器来匹配具有任何测试方法的类?

关于java - Java 中的方法重载 : The method is not applicable for arguments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12266101/

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