在 Void 方法上缺少返回值?

2022-09-03 10:15:01

下面的代码给了我编译时错误:缺少返回值和缺少返回语句,我将为此Void类型返回什么值?

final SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
    @Override
    protected Void doInBackground() throws Exception {
        // some code
        if (something) {
            return;
        }
    }
}

答案 1

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
 */

如果需要,则需要在末尾添加语句。Voidreturn

例:

protected Void doInBackground() throws Exception {
    // some code
    if (something) {
       return null;
    }
    return null;
}

答案 2

检查一下。它要求返回大写的“V”而不是简单的Voidvoid

final SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
    @Override
    protected Void doInBackground() throws Exception {
        // my code here
        return null;
    }
}

推荐