简体   繁体   English

我应该如何在我的方法的 arguments 中定义 lambda?

[英]How should I define a lambda in my method's arguments?

Ι have the following custom Runnable: Ι 有以下自定义Runnable:

class RandomSum extends Runnable {

   public void run() {
     float sum - 0;
     for(int i = 0; i<1000000;i++){
       sum+=Math.random();
     } 
   }
}

And I want to run it like that:我想这样运行它:


  RandomSum s =new RandomSum();
  s.retrieveCallback((sum)->{
    System.out.println(sum);
  });
  Thread thread = new Thread();
  thread.start();

But I do not know how I should define the method retrieveCallback in RandomSum that accepts a lambda?但我不知道我应该如何在接受 lambda 的 RandomSum 中定义方法retrieveCallback

One possible target type of the lambda you've shown is a Consumer .您展示的 lambda 的一种可能目标类型Consumer Define a method setCallback accepting a Consumer<Float> and store it as an instance variable.定义一个接受Consumer<Float>的方法setCallback并将其存储为实例变量。 Then invoke it after the for loop.然后在for循环之后调用它。

class RandomSum implements Runnable {
    Consumer<Float> callback;
   
    public void setCallback(Consumer<Float> callback) {
        this.callback = callback;
    }

    public void run() {
        float sum = 0;
        for(int i = 0; i<1000000;i++){
            sum += Math.random();
        } 
        callback.accept(sum);
    } 
}

Caller side主叫方

RandomSum s =new RandomSum();
s.setCallback((sum)->{
    System.out.println(sum);
});

Or using method references,或者使用方法引用,

s.setCallback(System.out::println);

Preferably you can pass the callback in the constructor.最好您可以在构造函数中传递回调。

You can define retrieveCallback within RandomSum as follows:您可以在RandomSum中定义retrieveCallback ,如下所示:

public void retrieveCallback(FeedbackHandler feedbackHandler) {
    int sum = ...; // Get the value however you like.
    feedbackHandler.handleFeedback(sum);
}

And then define this FeedbackHandler interface as:然后将这个FeedbackHandler接口定义为:

public interface FeedbackHandler {
    void handleFeedback(int sum);
}

Essentially what happens when you pass lambda (sum) -> {...} to retrieveCallback is:基本上,当您将 lambda (sum) -> {...}传递给retrieveCallback时会发生什么:

retrieveCallback(new FeedbackHandler() {
    @Override
    public void handleFeedback(int sum) {
        ...
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Java 8 中定义一个以 lambda 作为参数的方法? - How do I define a method which takes a lambda as a parameter in Java 8? 如何定义第一次调用我的 @Scheduled 方法的时间? - How to define when my @Scheduled method should be called first time? 我应该如何定义一个返回数组的集合方法? - How should I define a collection method that returns an array? 我应该如何定义我的模型类与Gson的安排? - How should I define my model class for an arrangement with Gson? 如何为传递给方法的流利的lambda定义订单 - How to define an order for a fluent lambda passed to a method 如何定义一个方法,该方法接收两个类型T和S的参数,它们在Java中扩展了T? - How to define a method that receives two arguments of type T, and S which extends T in java? 我应该比较班级“等于”方法中的所有字段吗? - Should I compare all fields in my class's “equals” method? 通用方法 如何用我的 class 类型定义? - Generic method How can I define with my class type? 我的方法标题对于它的返回类型有多具体? - How specific should my method header be about it's return type? 我应该同步我的方法吗? - Should I synchronize my method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM