Java 8方法引用静态void方法

vac*_*ach 5 java java-8 method-reference

有没有办法引用返回void的静态方法?

我试过这个

public Function<Runnable, Void> runner = Platform::runLater;
Run Code Online (Sandbox Code Playgroud)

但它会说"错误的返回类型,无法将void转换为java.lang.Void"

Era*_*ran 13

如果您的方法没有返回值,请不要使用该Function接口.

请改用消费者<Runnable>.

public Consumer<Runnable> runner = Platform::runLater;
Run Code Online (Sandbox Code Playgroud)

represents an operation that accepts a single input argument and returns no result.

  • 但是对于这个特定情况,您可能更喜欢[`Executor`](http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executor.html),因为它是自然的`Runnable - >无效的界面.这是特别方便的,因为已经有API方法接受一个有意义的`Executor`. (6认同)