java Consumer<>_具有多个参数的Java 8 Consumer在哪里?

小智..

5

为自己定义功能接口非常容易。在我目前正在从事的项目中,我发现有几次通过将方法作为参数传递使我能够保持代码的美观和DRY。这是我定义的一些功能接口:

@FunctionalInterface

public interface CheckedVoidFunction {

void apply() throws Exception;

}

@FunctionalInterface

public interface VoidFunction {

void apply();

}

@FunctionalInterface

public interface SetCategoryMethod {

void accept(ObservableList list, Document document, String pattern);

}

这是一种接受此类参数的方法:

private void loadAnnotations(String annotationType, VoidFunction afterLoadFunction, List documentPaths) {

在这里,我用不同的方法作为参数来调用这些方法:

loadAnnotations(DocumentReader.REDACTION_ANNOTATION_TYPE, this::afterLoadRedactions, documentPaths);

loadAnnotations(DocumentReader.HIGHLIGHT_ANNOTATION_TYPE, this::afterLoadHighlights, documentPaths);

也许有更好的方法可以做到这一点,但我想我会分享对我有用的东西。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值