这就是我的问题.我有一个带有几个getter方法和一个实现类的接口.
Class MyInterface {
String getA();
String getB();
...
}
Class MyClass implements MyInterface {
}
我需要编写一些逻辑来比较MyClass的两个对象之间的Get方法的值.
static List<Function> getters = List.of(MyInterface::getA, MyInterface::getB, ...); //this is hard-coded
for(Function getter : getters) {
if(getter.apply(obj1).equals(getter.apply(obj2))
//do something
}
无论我怎么try ,我似乎都不能得到正确的语法,并满足"类型"神.请帮帮忙.
使用@FunctionalInterface,我能够将列表定义为成员变量,而不是静态变量,如下所示.
@FunctionalInterface
interface GetterFunction {
String get();
}
List<GetterFunction> allGetters = List.of(this::getA, this::getB, ...)
但这看起来很难看,感觉还有更好的方法.
我也不想做反思.