gpt4 book ai didi

java - 谓词.and(Predicate) 不适用于参数 Predicate

转载 作者:行者123 更新时间:2023-12-01 07:00:30 29 4
gpt4 key购买 nike

我必须执行and()在我的实现 Predicate<MoneyOperation> 的业务对象上.

出现问题的代码是 and() 行调用:

        Predicate<? super MoneyOperation> predicate =
new MoneyOperationDateWithRadioButtonsPredicate(newv,
thisDayRdBtn.getValue(), date);

filteredData.setPredicate(filteredData.getPredicate().and(predicate));

错误:

The method and(Predicate<? super capture#14-of ? super MoneyOperation>) 
in the type Predicate<capture#14-of ? super MoneyOperation> is not applicable
for the arguments (Predicate<capture#15-of ? super MoneyOperation>)

虽然当我经过predicate时至setPredicate()无需调用and()编译器不会引发任何错误。

MoneyOperationDateWithRadioButtonsPredicate类:

public class MoneyOperationDateWithRadioButtonsPredicate extends MoneyOperationFieldPredicate<LocalDate> { ... }

MoneyOperationFieldPredicate类:

public abstract class MoneyOperationFieldPredicate<T> implements Predicate<MoneyOperation> { ... }

最佳答案

这里的问题是捕获。让我们看看这些限制意味着什么:

Predicate<? super MoneyOperation>是一个可以采用 MoneyOperation 的谓词或者它可能是父类(super class)并返回一个 boolean 值。这意味着它可以是 Predicate<Object>这需要 Object并返回一个 boolean 值。

现在有一个潜在的问题:
我们假设MoneyOperation延伸GenericOperation并实现SomeInterface .
第一个Predicate<? super MoneyOperation>实际上可能是 Predicate<GenericOperation>第二个Predicate<? super MoneyOperation>一个Predicate<SomeInterface> 。 (Java编译器无法分辨)通过MoneyOperation对它们两者都很好,但是将它们组合起来需要将其类型限制为 Predicate<MoneyOperation> 。虽然这对于 Predicate 来说没问题。 ,其他情况可能不适用。

这就是 Java 不允许这样做的原因。 使用Predicate<MoneyOperation>如果您知道它是其中之一。

关于java - 谓词<? super X>.and(Predicate<? super X>) 不适用于参数 Predicate<? super X>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57649578/

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