以下代码给我编译时错误:缺少返回值和缺少 return 语句,对于此 Void 类型
,我会返回什么值?
final SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
@Override
protected Void doInBackground() throws Exception {
// some code
if (something) {
return;
}
}
}
最佳答案
void
不是void
,如果不想返回就改成void类型
void 是类,void 是类型。
/**
* The {@code Void} class is an uninstantiable placeholder class to hold a
* reference to the {@code Class} object representing the Java keyword
* void.
*
* @author unascribed
* @since JDK1.1
*/
如果你想要Void
,那么你需要在最后添加return
语句。
例子:
protected Void doInBackground() throws Exception {
// some code
if (something) {
return null;
}
return null;
}
关于java - Void 方法缺少返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13749612/