2 回答

TA贡献1829条经验 获得超13个赞
这是我为你发明的。
定义
public interface IndexBytePairConsumer {
void accept(long index, byte value);
}
public interface IndexIntPairConsumer extends IndexBytePairConsumer {
default void accept(long index, byte value) {
this.accept(index, (int) value);
}
void accept(long index, int value);
}
你可以使用它
IndexIntPairConsumer c = (a,b)->{
System.out.println(a + b);
};
forEachIndexValuePair(c);
forEachIndexValuePair((a, b) -> {
System.out.println(a + b);
});

TA贡献1810条经验 获得超5个赞
在不更改类型层次结构的情况下(例如,此答案中建议的方式),适应步骤是不可避免的,因为IndexBytePairConsumer它们IndexIntPairConsumer是两种不同的类型。最小的适应步骤是
// given
IndexIntPairConsumer consumer = …
// call as
forEachIndexValuePair(consumer::accept);
正如您在问题中所说,int 的使用者可以接受字节,因此acceptan的方法是预期IndexIntPairConsumeran 的方法引用的有效目标。IndexBytePairConsumer
添加回答
举报