当前位置:网站首页>lombok @Builder注解
lombok @Builder注解
2022-07-05 18:21:00 【fastjson_】
@Builder注解的使用
@Builder
public class Card {
private int id;
private String name;
private boolean sex;
}Card card = Card.builder().
id(10).name("dasd").sex(true).
build();优点
- 不需些太多的set方法来定义属性内容
- 写法更优雅
@Builder对类做了什么?
我们可以反编译生成的 Card.class
public class Card {
private int id;
private String name;
private boolean sex;
Card(int id, String name, boolean sex) {
this.id = id;
this.name = name;
this.sex = sex;
}
public static Card.CardBuilder builder() {
return new Card.CardBuilder();
}
public static class CardBuilder {
private int id;
private String name;
private boolean sex;
CardBuilder() {
}
public Card.CardBuilder id(int id) {
this.id = id;
return this;
}
public Card.CardBuilder name(String name) {
this.name = name;
return this;
}
public Card.CardBuilder sex(boolean sex) {
this.sex = sex;
return this;
}
public Card build() {
return new Card(this.id, this.name, this.sex);
}
public String toString() {
return "Card.CardBuilder(id=" + this.id + ", name=" + this.name + ", sex=" + this.sex + ")";
}
}
}
那么其实很明显了,注解在编译后使得Card类中多了一个名为Card.CardBuilder的静态内部类。这个静态类拥有和Card类相同的属性,并且他额外实现了一些方法:
1.name、sex、id等的属性方法
其实这些方法和setAttribute十分类似,只是额外返回了实例本身,这使得它可以使用类似于链式调用的写法。
2.build方法
该方法调用Card类的全参构造方法来生成Card实例。
Card类还是实现了builder方法,这个方法生成一个空的Card.CardBuilder实例
缺点
最明显的一点,在生成Card实例之前,实际上是先创建了一个Card.CardBuilder实例,这样很明显额外占用了内存。
额外
@Builder(toBuilder = true)
这个选项允许你将一个实例化好的Card,更新字段生成新的Card实例。
public Card.CardBuilder toBuilder() {
return (new Card.CardBuilder()).id(this.id).name(this.name).sex(this.sex);
}
public static void main(String[] args) {
Card card = Card.builder().id(10).name("奔驰").sex(true).build();
Card card1 = card.toBuilder().name("宝马").build();
System.out.println(card);
System.out.println(card1);
}Card(id=10, name=奔驰, sex=true)
Card(id=10, name=宝马, sex=true)
边栏推荐
- [QNX hypervisor 2.2 user manual]6.3.2 configuring VM
- 彻底理解为什么网络 I/O 会被阻塞?
- Whether to take a duplicate subset with duplicate elements [how to take a subset? How to remove duplicates?]
- 英语句式参考
- 【PaddlePaddle】 PaddleDetection 人脸识别 自定义数据集
- 【在优麒麟上使用Electron开发桌面应】
- Gimp 2.10 tutorial "suggestions collection"
- Cronab log: how to record the output of my cron script
- LeetCode 6111. 螺旋矩阵 IV
- Sophon Base 3.1 推出MLOps功能,为企业AI能力运营插上翅膀
猜你喜欢

@Extension, @spi annotation principle

Vulnhub's darkhole_ two

vulnhub之darkhole_2

记录Pytorch中的eval()和no_grad()

Trust counts the number of occurrences of words in the file

Tupu software digital twin | visual management system based on BIM Technology

Introduction to the development function of Hanlin Youshang system of Hansheng Youpin app

《ClickHouse原理解析与应用实践》读书笔记(5)

Fix vulnerability - mysql, ES

星环科技重磅推出数据要素流通平台Transwarp Navier,助力企业实现隐私保护下的数据安全流通与协作
随机推荐
vulnhub之darkhole_2
MATLAB中print函数使用
websocket 工具的使用
How to improve the thermal management in PCB design with the effective placement of thermal through holes?
Sophon CE社区版上线,免费Get轻量易用、高效智能的数据分析工具
写作写作写作写作
使用JMeter录制脚本并调试
Fix vulnerability - mysql, ES
记一次使用Windbg分析内存“泄漏”的案例
最大人工岛[如何让一个连通分量的所有节点都记录总节点数?+给连通分量编号]
《力扣刷题计划》复制带随机指针的链表
Simulate the hundred prisoner problem
Is it safe for golden sun to open an account? Can I open an account free of 5 in case?
Crontab 日志:如何记录我的 Cron 脚本的输出
Failed to virtualize table with JMeter
音视频包的pts,dts,duration的由来.
消除`if()else{ }`写法
Generate classes from XML schema
New words new words new words new words [2]
How to obtain the coordinates of the aircraft passing through both ends of the radar