Java:参数化可运行

发布于 2024-08-09 20:48:11 字数 335 浏览 10 评论 0 原文

标准的 Runnable 接口只有非参数化的 run() 方法。还有 Callable 接口,其中 call() 方法返回泛型类型的结果。我需要传递通用参数,如下所示:

interface MyRunnable<E> {
  public abstract void run(E reference);
}
Is there any standard interface for this purpose or I must declare that basic one by myself?

Standard Runnable interface has only non-parametrized run() method. There is also Callable<V> interface with call() method returning result of generic type. I need to pass generic parameter, something like this:

interface MyRunnable<E> {
  public abstract void run(E reference);
}


Is there any standard interface for this purpose or I must declare that basic one by myself?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

抠脚大汉 2024-08-16 20:48:11

通常,您可以使用支持通用输入参数的类来实现 Runnable 或 Callable ;例如

public class MyRunnable<T> implements Runnable {
  private final T t;

  public MyRunnable(T t) {
    this.t = t;
  }

  public void run() {
    // Reference t.
  }
}

Typically you would implement Runnable or Callable with a class that supports a generic input parameter; e.g.

public class MyRunnable<T> implements Runnable {
  private final T t;

  public MyRunnable(T t) {
    this.t = t;
  }

  public void run() {
    // Reference t.
  }
}
傲鸠 2024-08-16 20:48:11

Java 8 包括 java.util.function.Consumer ; 与单个非默认方法voidaccept(T t) 的接口。

该包中还有许多其他相关接口

Java 8 includes the java.util.function.Consumer<T> interface with the single non-default method void accept(T t).

There are many other related interfaces in that package.

苍景流年 2024-08-16 20:48:11

还有 com.google.common.base.Function,来自 Google 集合番石榴

如果将输出类型设置为 ?Void(并且始终返回 null),则可以将其用作 的替代方案带有输入参数的可运行

这样做的优点是能够使用 Functions.compose 来转换输入值,Iterables.transform 将其应用到集合的每个元素等。

There is also com.google.common.base.Function<F, T> from Google CollectionsGuava.

If you set the output type to ? or Void (and always have it return null) you can use it as an alternative to Runnable with an input parameter.

This has the advantage of being able to use Functions.compose to transform the input value, Iterables.transform to apply it to every element of a collection etc.

少跟Wǒ拽 2024-08-16 20:48:11

一般来说,如果您想将参数传递给 run() 方法,您将使用带有参数的构造函数对 Runnable 进行子类化。

例如,您想要这样做:

// code
Runnable r = new YourRunnable();
r.run(someParam);
//more code

您需要这样做:

// code
Runnable r = new YourRunnable(someParam);
r.run();
//more code

您将实现类似于下面的 YourRunnable

public class YourRunnable implements Runnable {
    Some param;
    public YourRunnable(Some param){
        this.param = param;
    }
    public void run(){
        // do something with param
    }
}

Generally if you wanna pass a parameter into the run() method you will subclass Runnable with a constructor that takes a parameter.

For example, You wanna do this:

// code
Runnable r = new YourRunnable();
r.run(someParam);
//more code

You need to do this:

// code
Runnable r = new YourRunnable(someParam);
r.run();
//more code

You will implement YourRunnable similar to below:

public class YourRunnable implements Runnable {
    Some param;
    public YourRunnable(Some param){
        this.param = param;
    }
    public void run(){
        // do something with param
    }
}
单身狗的梦 2024-08-16 20:48:11

我建议像原始问题中那样定义一个接口。此外,通过使接口特定于其应该执行的操作来避免弱类型,而不是像 Runnable 这样无意义的接口。

I suggest defining an interface as done in the original question. Further, avoid weak typing by making the interface specific to what it is supposed to do, rather than a meaning-free interface like Runnable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文