当前位置:网站首页>Lombok @builder annotation
Lombok @builder annotation
2022-07-05 18:33:00 【fastjson_】
@Builder Use of annotations
@Builder
public class Card {
private int id;
private String name;
private boolean sex;
}
Card card = Card.builder().
id(10).name("dasd").sex(true).
build();
advantage
- It doesn't need too much set Method to define the property content
- More elegant writing
@Builder What's done to the class ?
We can decompile the generated 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 + ")";
}
}
}
Well, it's obvious , Annotations are compiled to make Card There is one more class named Card.CardBuilder The static inner class of . This static class has and Card Class has the same properties , And he implemented some extra methods :
1.name、sex、id And so on
In fact, these methods and setAttribute Very similar , It's just the extra return of the instance itself , This makes it possible to write something like a chain call .
2.build Method
This method calls Card Class to generate Card example .
Card Class is still implemented builder Method , This method generates an empty Card.CardBuilder example
shortcoming
The most obvious point , It's generating Card Before instance , In fact, we created a Card.CardBuilder example , This obviously takes up extra memory .
additional
@Builder(toBuilder = true)
This option allows you to instantiate a good Card, Update fields to generate new Card example .
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(" Mercedes ").sex(true).build();
Card card1 = card.toBuilder().name(" BMW ").build();
System.out.println(card);
System.out.println(card1);
}
Card(id=10, name= Mercedes , sex=true)
Card(id=10, name= BMW , sex=true)
边栏推荐
- U-Net: Convolutional Networks for Biomedical Images Segmentation
- Numerical calculation method chapter8 Numerical solutions of ordinary differential equations
- About Statistical Power(统计功效)
- ConvMAE(2022-05)
- About Estimation with Cross-Validation
- Leetcode notes: Weekly contest 300
- [QNX hypervisor 2.2 user manual]6.3.2 configuring VM
- 兄弟组件进行传值(显示有先后顺序)
- 项目中遇到的问题 u-parse 组件渲染问题
- Privacy computing helps secure data circulation and sharing
猜你喜欢
The main thread anr exception is caused by too many binder development threads
rust统计文件中单词出现的次数
How to obtain the coordinates of the aircraft passing through both ends of the radar
About statistical power
websocket 工具的使用
Tupu software digital twin | visual management system based on BIM Technology
分享:中兴 远航 30 pro root 解锁BL magisk ZTE 7532N 8040N 9041N 刷机 刷面具原厂刷机包 root方法下载
《ClickHouse原理解析与应用实践》读书笔记(5)
The 11th China cloud computing standards and Applications Conference | China cloud data has become the deputy leader unit of the cloud migration special group of the cloud computing standards working
Wu Enda team 2022 machine learning course, coming
随机推荐
写作写作写作写作
Memory leak of viewpager + recyclerview
兄弟组件进行传值(显示有先后顺序)
How to improve the thermal management in PCB design with the effective placement of thermal through holes?
Gimp 2.10 tutorial "suggestions collection"
How can cluster deployment solve the needs of massive video access and large concurrency?
常见时间复杂度
Introduction to VC programming on "suggestions collection"
Reading notes of Clickhouse principle analysis and Application Practice (5)
【PaddleClas】常用命令
The main thread anr exception is caused by too many binder development threads
吴恩达团队2022机器学习课程,来啦
The easycvr platform reports an error "ID cannot be empty" through the interface editing channel. What is the reason?
Notes on common management commands of openshift
Le cours d'apprentissage de la machine 2022 de l'équipe Wunda arrive.
Nacos distributed transactions Seata * * install JDK on Linux, mysql5.7 start Nacos configure ideal call interface coordination (nanny level detail tutorial)
Is it safe to open an account, register and dig money? Is there any risk? Is it reliable?
rust统计文件中单词出现的次数
Deep copy and shallow copy [interview question 3]
深入底层C源码讲透Redis核心设计原理