龙目岛(Lombok)Getter/Setter vs Java 14记录 [英] Lombok getter/setter vs Java 14 record

查看:134
本文介绍了龙目岛(Lombok)Getter/Setter vs Java 14记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢项目 Lombok ,但是如今,我正在阅读并尝试Java 14的一些新功能.

I love project Lombok but in these days I'm reading and trying some of the new features of java 14.

在新功能内,还有一个 record 关键字,它允许使用已构建的类创建类-具有以下功能:构造函数,私有最终字段,访问器,equals/hashCode,getter和toString方法.

Inside the new capability, there is the record keyword that allows creating a class with already built-in the following functionality: constructor, private final fields, accessors, equals/hashCode, getters, toString methods.

现在我的问题是:最好依靠Lombok的功能,还是我们应该开始使用记录功能:

Now my question is: is better to rely on the feature of Lombok or should we start using the record functionality:

最好使用这个:

record Person (String name, String surname) {}

或那个:

@AllArgsConstructor
@ToString
@EqualsAndHashCode
public class GetterSetterExample {
  @Getter private int name;
  @Getter private int surname;
}

两种方法的优缺点是什么?

What are the pros and cons of the both approach?

推荐答案

Lombok和Java语言的record功能是用于不同目的的不同工具.有一些表面上的重叠,但不要让那分散您的注意力.

Lombok, and the record feature of the Java language, are different tools for different things. There is some superficial overlap, but don't let that distract you.

龙目岛很大程度上是关于语法的便利;它是一个预装有一些已知有用代码模式的宏处理器.它没有赋予任何语义.根据您在代码中设置的带有注释的旋钮,它只会自动执行模式. Lombok纯粹是关于实现数据承载类的便利.

Lombok is largely about syntactic convenience; it is a macro-processor pre-loaded with some known useful patterns of code. It doesn't confer any semantics; it just automates the patterns, according to some knobs you set in the code with annotations. Lombok is purely about the convenience of implementing data-carrying classes.

记录是一种语义功能;它们是标称元组.通过做出Point (int x, int y)的元组的语义声明,编译器可以从该状态导出其表示以及构造,声明,相等,哈希和字符串表示协议描述.由于它们具有语义,因此读者和框架也可以对记录的API更有把握地进行推理. (这在语法上也可能很方便;如果是这样,那就太好了.)

Records are a semantic feature; they are nominal tuples. By making a semantic declaration that Point is a tuple of (int x, int y), the compiler can derive its representation, as well as construction, declaration, equality, hashing, and string representation protocols, from this state description. Because they carry semantics, readers and frameworks can also reason with higher confidence about the API of records. (This may also be syntactically convenient; if so, that's great.)

这篇关于龙目岛(Lombok)Getter/Setter vs Java 14记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆