当前位置:网站首页>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)
边栏推荐
- How to choose the most formal and safe external futures platform?
- Logical words in Articles
- SAP feature description
- Introduction to VC programming on "suggestions collection"
- Is it complicated to open an account? Is online account opening safe?
- Electron安装问题
- Maximum artificial island [how to make all nodes of a connected component record the total number of nodes? + number the connected component]
- The 10th global Cloud Computing Conference | Huayun data won the "special contribution award for the 10th anniversary of 2013-2022"
- Use of print function in MATLAB
- buuctf-pwn write-ups (9)
猜你喜欢

Record a case of using WinDbg to analyze memory "leakage"

Privacy computing helps secure data circulation and sharing

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

U-Net: Convolutional Networks for Biomedical Images Segmentation

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

Insufficient picture data? I made a free image enhancement software

ViewPager + RecyclerView的内存泄漏

Failed to virtualize table with JMeter

使用JMeter录制脚本并调试

FCN: Fully Convolutional Networks for Semantic Segmentation
随机推荐
吳恩達團隊2022機器學習課程,來啦
彻底理解为什么网络 I/O 会被阻塞?
SAP feature description
7-2 保持链表有序
爱因斯坦求和einsum
Maximum artificial island [how to make all nodes of a connected component record the total number of nodes? + number the connected component]
About Estimation with Cross-Validation
【PaddleClas】常用命令
JDBC reads a large amount of data, resulting in memory overflow
Login and connect CDB and PDB
websocket 工具的使用
Electron安装问题
《2022中国信创生态市场研究及选型评估报告》发布 华云数据入选信创IT基础设施主流厂商!
Copy the linked list with random pointer in the "Li Kou brush question plan"
About statistical power
生词生词生词生词[2]
Notes on common management commands of openshift
Is it safe for golden sun to open an account? Can I open an account free of 5 in case?
[paddleclas] common commands
Sibling components carry out value transfer (there is a sequence displayed)