gpt4 book ai didi

java - 对于始终为 null 的方法参数,NullType 与 Void

转载 作者:行者123 更新时间:2023-11-29 02:58:50 25 4
gpt4 key购买 nike

我有一些由框架调用的通用处理器。参数是我处理的有效负载。

interface Processor<T, U> {
U process(T t);
}

有些情况下有效载荷没有意义且不存在(仅计算并返回响应)。在这种情况下,框架将 null 作为参数传递。我决定使用 javax.lang.model.type.NullType为此目的:

new Processor<NullType, String>() {
public String process(NullType unused) {
...
return result;
}
}

我在这里滥用了 NullType 的意图吗?是java.lang.Void还是其他更合适的东西?

最佳答案

在这些情况下我更喜欢 Void,这使得传递 null 成为唯一有效的参数。

NullType 另一方面可以实例化。


出于好奇,it is possible to instantiate an object of type Void .

关于java - 对于始终为 null 的方法参数,NullType 与 Void,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36498459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com