相关疑难解决方法(0)

返回一个Void对象

返回Void类型的正确方法是什么,当它不是原始类型时?例如.我目前使用null如下.

interface B<E>{ E method(); }

class A implements B<Void>{

    public Void method(){
        // do something
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

java generics return-value

104
推荐指数
2
解决办法
9万
查看次数

为什么我不能从方法中显式返回void?

void run() {
    ...
    if (done) return cancel();
    ...
}
Run Code Online (Sandbox Code Playgroud)

哪里cancel()回来void.这不会编译......我几乎可以理解为什么.但是如果我想从虚空中归还一个空洞,为什么不呢?相反,我最终写了这样的东西:

if (done) {
    cancel();
    return;
}
Run Code Online (Sandbox Code Playgroud)

我不是在寻找代码风格的建议,我想知道为什么Java明确禁止这种类型的void返回.任何信息表示赞赏,谢谢.

java return-type void

55
推荐指数
5
解决办法
6381
查看次数

标签 统计

java ×2

generics ×1

return-type ×1

return-value ×1

void ×1