java - Void 方法缺少返回值?

标签 java generics

以下代码给我编译时错误:缺少返回值和缺少 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/

相关文章:

c# - 通用约束忽略协方差

java - 为什么在使用有界类型参数时需要强制转换

java - Java 中的泛型还是继承/接口(interface)?

java - 如何使用 smack java 库向 gtalk 联系人发送邀请?

java - 如何解决代码中的不兼容类型

java - maven如何处理具有不同group-id的同一个jar

java - 我可以有一个接受子类型的列表属性吗?

Java自动鼠标

Java修复代码中的正则表达式

Java 泛型编译错误