java - 如何在java中使用lambda回傳一個物件?我嘗試使用 objeto = (Runnable[] obj, int pos) -> {obj[pos ]};inmain()但不起作用。
是否可以obj[pos]在 inst() 函式中使用 lambda 回傳物件?
import java.util.Comparator;
public class Runna{
public static Runnable inst(Runnable[] obj, int pos){
return obj[pos];
}
}
uj5u.com熱心網友回復:
聽起來你想這樣做。
Runnable[] array = new Runnable[10]; // some array filled with Runnables.
BiFunction<Runnable[], Integer, Runnable> lookup = (arr, i) -> arr[i];
Runnable foo = lookup.apply(array, 3);
現在你可以做foo.run();
您需要 aBiFunction接受兩個不同的引數并回傳第三個。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/481909.html
