是否存在类似于Callable但带参数的接口? [英] Is there an interface similar to Callable but with arguments?
问题描述
Java中的接口是否类似于 Callable
接口,它可以接受其调用方法的参数?
Is there an interface in Java similar to the Callable
interface, that can accept an argument to its call method?
像这样:
public interface MyCallable<V> {
V call(String s) throws Exception;
}
如果已经存在我可以存在的东西,我宁愿避免创建新类型使用。或者是否有更好的策略让多个客户端实现并插入可调用的例程?
I would rather avoid creating a new type if there already exists something that I can use. Or is there a better strategy to having multiple clients implement and plug in a callable routine?
从这里复制http://www.programmingforums.org/thread27905.html
推荐答案
自Java以来8在 java.util.function
包。你要求的具体是 功能
。
Since Java 8 there is a whole set of Function-like interfaces in the java.util.function
package. The one you're asking for specifically is simply Function
.
在Java 8之前,没有通用的内置接口这个,但有些图书馆提供了它。
Prior to Java 8, there was no general-purpose, built-in interface for this, but some libraries provided it.
例如番石榴 函数< F,T>
界面,方法 T apply(F输入)
。它还在几个地方大量使用该界面。
For example Guava has the Function<F,T>
interface with the method T apply(F input)
. It also makes heavy use of that interface in several places.
这篇关于是否存在类似于Callable但带参数的接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!