Boh*_*ian 5 java generics coding-style
假设需要使用特定的通用接口,但这种情况不需要使用其中一个通用参数.
假设我需要一个Callable<T>(必须T从其call()方法返回一个),但在这种情况下我不需要返回结果,我只想提交一些代码ExecutorService来"做某事"
什么是该类型的最佳选择T?
您可以使用特殊Void类型:
Callable<Void> callable = new Callable<Void>() {
@Override
public Void call() throws Exception {
// do stuff
return null;
}
};
Run Code Online (Sandbox Code Playgroud)
return退出方法需要该语句.编译器接受的唯一值是null.相当方便!