【发布时间】:2020-06-01 07:14:57
【问题描述】:
我想将非空值注入到我的 groovy 类中
class MyClass {
private final String foo
private final Integer bar
MyClass(String foo, Integer bar) {
// wanted ctr body
}
...
}
在构造函数中,我想将参数分配给各个字段 AND 防止空值。
我想知道是否有比相当冗长的更时髦的方式来做到这一点
assert foo != null
assert bar != null
this.foo = foo
this.bar = bar
或
this.foo = Objects.requireNonNull foo
this.bar = Objects.requireNonNull bar
【问题讨论】:
标签: groovy