java - 将 Predicate<> 转换为子类型
问题描述
我有一个Predicate
我想在某些操作中使用的自定义实现。但是,我有一种很难使用多态性的类型。
经过一番调查,我在下面编写了最小的代码来重现问题(这是对问题的更好解释,比我能描述的还要好)。
class Custom implements Predicate<Integer> {
int y;
public Custom(int y) {
this.y = y;
}
@Override
public boolean test(Integer i) {
return y+i>0;
}
}
public class Main{
public static void main(String[] args) {
Custom c1 = new Custom(5);
Custom c2 = new Custom(8);
Custom c = (Custom) c1.and(c2); // faulty line - unable to cast
}
}
我不确定为什么铸造失败以及如何使其发挥作用。
解决方案
如果您想保留Custom
对象的状态并实现Predicate
接口,我建议重载and
,or
和negate
方法。当您将两个Custom
对象与结合时and
,or
或者当您调用时,negate
您将获得一个Custom
对象作为返回值。当您将Custom
对象与Predicate<Integer
方法的任何其他实现结合起来时,仍然会返回Predicate<Integer
:
class Custom implements Predicate<Integer> {
class And extends Custom {
Custom a;
Custom b;
public And(Custom a, Custom b) {
super((i) -> a.test(i) && b.test(i));
this.a = a;
this.b = b;
}
}
class Or extends Custom {
Custom a;
Custom b;
public Or(Custom a, Custom b) {
super((i) -> a.test(i) || b.test(i));
this.a = a;
this.b = b;
}
}
class Not extends Custom {
Custom custom;
public Not(Custom custom) {
super((i) -> !custom.test(i));
this.custom = custom;
}
}
private final Predicate<Integer> predicate;
public Custom(int y) {
this((i) -> y + i > 0);
}
private Custom(Predicate<Integer> predicate) {
this.predicate = predicate;
}
@Override
public boolean test(Integer i) {
return predicate.test(i);
}
public Custom.And and(Custom other) {
return new Custom.And(this, other);
}
public Custom.Or or(Custom other) {
return new Custom.Or(this, other);
}
public Custom.Not negate() {
return new Custom.Not(this);
}
}
推荐阅读
- flutter - 有没有办法在颤动中选择默认键盘语言?
- python - ModuleNotFoundError:没有名为“字母”的模块
- c# - Blazor 的 Web api 基本 URL 的全局变量
- java - java中枚举值的条件
- vue.js -
占用比必要更多的空间(vuetify) - python - 将 pandas DataFrame 与 PyQt5 QTableView 同步
- python - Pyramid 中同一路线具有不同上下文的多个视图
- node.js - 不解的猫鼬
- javascript - 可以在第一次渲染时映射,但在刷新显示时无法映射未定义
- json - 在没有未来/异步的情况下解析本地 .json 同步?