当前位置:网站首页>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)
边栏推荐
- 个人对卷积神经网络的理解
- Thoroughly understand why network i/o is blocked?
- U-Net: Convolutional Networks for Biomedical Images Segmentation
- The easycvr platform reports an error "ID cannot be empty" through the interface editing channel. What is the reason?
- Quickly generate IPA package
- 【在优麒麟上使用Electron开发桌面应】
- 第十届全球云计算大会 | 华云数据荣获“2013-2022十周年特别贡献奖”
- 深入底层C源码讲透Redis核心设计原理
- LeetCode 6111. 螺旋矩阵 IV
- 第十一届中国云计算标准和应用大会 | 华云数据成为全国信标委云计算标准工作组云迁移专题组副组长单位副组长单位
猜你喜欢
Use QT to traverse JSON documents and search sub objects
ConvMAE(2022-05)
How to obtain the coordinates of the aircraft passing through both ends of the radar
LeetCode 6109. Number of people who know the secret
The 10th global Cloud Computing Conference | Huayun data won the "special contribution award for the 10th anniversary of 2013-2022"
Thoroughly understand why network i/o is blocked?
Trust counts the number of occurrences of words in the file
The 2022 China Xinchuang Ecological Market Research and model selection evaluation report released that Huayun data was selected as the mainstream manufacturer of Xinchuang IT infrastructure!
基于can总线的A2L文件解析(3)
使用JMeter录制脚本并调试
随机推荐
吳恩達團隊2022機器學習課程,來啦
websocket 工具的使用
英语句式参考
FCN: Fully Convolutional Networks for Semantic Segmentation
Quickly generate IPA package
Can communication of nano
兄弟组件进行传值(显示有先后顺序)
sample_ What is the relationship between rate, sample and duration
记录Pytorch中的eval()和no_grad()
【HCIA-cloud】【1】云计算的定义、什么是云计算、云计算的架构与技术说明、华为云计算产品、华为内存DDR配置工具说明
如何获取飞机穿过雷达两端的坐标
LeetCode 6111. 螺旋矩阵 IV
Is it safe for golden sun to open an account? Can I open an account free of 5 in case?
LeetCode 6109. Number of people who know the secret
Introduction to Resampling
Simulate the hundred prisoner problem
Maximum artificial island [how to make all nodes of a connected component record the total number of nodes? + number the connected component]
Share: ZTE Yuanhang 30 Pro root unlock BL magick ZTE 7532n 8040n 9041n brush mask original brush package root method Download
Tupu software digital twin | visual management system based on BIM Technology
The 10th global Cloud Computing Conference | Huayun data won the "special contribution award for the 10th anniversary of 2013-2022"