假设我在Java 8中有以下功能接口:
interface Action<T, U> {
U execute(T t);
}
在某些情况下,我需要一个没有参数或返回类型的操作。所以我写
就像这样:
Action<Void, Void> a = () -> { System.out.println("Do nothing!"); };
但是,它给了我编译错误,我需要把它写成
Action<Void, Void> a = (Void v) -> { System.out.println("Do nothing!"); return null;};
这很难看。是否有任何方法可以摆脱Void
类型参数?