【java函数式编程】如何降级BiConsumer成Consumer甚至是无参方法?

卡牌游戏开发中,使用效果时有两种模式,一种是选择对象发动能力,一种是输入序号发动抉择效果。

可能两种都有,可能两种都没有。如果【使用效果】对象是用BiConsumer来处理效果,那么如果两种都没有时,就要传一堆垃圾参数,看上去就很难受。

不多说,我是这样写的:


        public record Play(Supplier<List<GameObj>> targets, int targetNum,
        					int choiceNum, BiConsumer<Integer,List<GameObj>> effect){
            /**
             * 仅抉择
             */
            public Play(int choiceNum,Consumer<Integer> effect) {
                this(ArrayList::new,0,choiceNum,((integer, gameObjs) -> effect.accept(integer)));
            }
            /**
             * 仅选择目标
             */
            public Play(Supplier<List<GameObj>> targets, int targetNum,Consumer<List<GameObj>> effect) {
                this(targets,targetNum,0,((integer, gameObjs) -> effect.accept(gameObjs)));
            }
            /**
             * 不需要抉择/选择目标
             */
            public Play(FunctionN effect) {
                this(ArrayList::new,0,0,((integer, gameObjs) -> effect.apply()));
            }
        }

其中FunctionN 是自己写的一个函数类,代表无入参无出参,补齐了jdk中函数类的一个痛点。


@FunctionalInterface
public interface FunctionN {
    void apply();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值