我想在测试类中创建一个 Lombok 类
@RunWith(SpringRunner.class)
@SpringBootTest
public class HostelIntegrationTest {
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(NON_NULL)
@EqualsAndHashCode
class User {
String property1;
Instant property2;
Integer property3;
}
Run Code Online (Sandbox Code Playgroud)
但我收到此编译错误:
此处不允许使用修饰符 static
deh*_*asi 24
@Builderstatic在里面创建一个内部类。问题可能是非静态内部类中的静态内部类。
尽量使User还static
//other annotations
@Builder
static class User {
String property1;
Instant property2;
Integer property3;
}
Run Code Online (Sandbox Code Playgroud)
将内部类定义为static将解决此问题。
背景:内部类上的每个实例都将引用创建它的外部类的对象,除非内部类被定义为静态。通常你不需要那个引用,这就是为什么你应该将你的内部类定义为静态(与静态方法和字段不同,即使来自 OOP 的 PoV,这也是一个很好的静态)。
Lombok@Builder将在您的内部类 ( builder()) 中定义一个静态方法,该方法只允许在静态内部类中使用。
| 归档时间: |
|
| 查看次数: |
6465 次 |
| 最近记录: |