2 回答

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超13個(gè)贊
這是我為你發(fā)明的。
定義
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貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超5個(gè)贊
在不更改類(lèi)型層次結(jié)構(gòu)的情況下(例如,此答案中建議的方式),適應(yīng)步驟是不可避免的,因?yàn)镮ndexBytePairConsumer它們IndexIntPairConsumer是兩種不同的類(lèi)型。最小的適應(yīng)步驟是
// given
IndexIntPairConsumer consumer = …
// call as
forEachIndexValuePair(consumer::accept);
正如您在問(wèn)題中所說(shuō),int 的使用者可以接受字節(jié),因此acceptan的方法是預(yù)期IndexIntPairConsumeran 的方法引用的有效目標(biāo)。IndexBytePairConsumer
添加回答
舉報(bào)