`@value` 在这个例子中是如何在 Lombok 中工作的?

huangapple 未分类评论47阅读模式
英文:

How does @value in lombok work in this example

问题

以下是翻译好的内容:

这是我的简单类

@Value
public class Task {
    private static int numberOfTasks = 0;
    private int id = ++numberOfTasks;
    private String name;
    private Timestamp startTime;
    private Timestamp endTime;
}

阅读@Value的文档,上面写着:

在实践中@Value相当于final @ToString @EqualsAndHashCode @AllArgsConstructor @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) @Getter

这意味着有一个AllArgsConstructor。然而,如果我尝试将字段设为final,编译器会报错说字段未初始化。

(尽管Lombok在@Value的情况下声称会将字段设为final)

有人可以帮忙解决一下吗?

这就是我的意思:

`@value` 在这个例子中是如何在 Lombok 中工作的?

英文:

This is my simple class

@Value
public class Task {
    private static int numberOfTasks = 0;
    private int id = ++numberOfTasks;
    private String name;
    private Timestamp startTime;
    private Timestamp endTime;
}

Reading the Documentation of @Value, It says:

In practice, @Value is shorthand for: final @ToString @EqualsAndHashCode @AllArgsConstructor @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) @Getter,

So that means there is a AllArguConstructor. However, If I try to make the field as final, I get a compiler error that the filed is not initialised.

(though also Lombok claims to make the field as final in the case of @Value)

Could anyone help here?

This is what I mean:

`@value` 在这个例子中是如何在 Lombok 中工作的?

huangapple
  • 本文由 发表于 2020年7月25日 22:43:02
  • 转载请务必保留本文链接:https://java.coder-hub.com/63089675.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定