当前位置:网站首页>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)
边栏推荐
- Access the database and use redis as the cache of MySQL (a combination of redis and MySQL)
- 【在优麒麟上使用Electron开发桌面应】
- Le cours d'apprentissage de la machine 2022 de l'équipe Wunda arrive.
- 如何获取飞机穿过雷达两端的坐标
- Failed to virtualize table with JMeter
- Writing writing writing
- Various pits of vs2017 QT
- The easycvr platform reports an error "ID cannot be empty" through the interface editing channel. What is the reason?
- JVM third talk -- JVM performance tuning practice and high-frequency interview question record
- Record a case of using WinDbg to analyze memory "leakage"
猜你喜欢

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

Thoroughly understand why network i/o is blocked?

Nacos distributed transactions Seata * * install JDK on Linux, mysql5.7 start Nacos configure ideal call interface coordination (nanny level detail tutorial)

如何写出好代码 - 防御式编程

解决 contents have differences only in line separators

吳恩達團隊2022機器學習課程,來啦

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

@Extension、@SPI注解原理

基于can总线的A2L文件解析(3)

怎么自动安装pythn三方库
随机推荐
开户注册股票炒股安全吗?有没有风险的?靠谱吗?
让更多港澳青年了解南沙特色文创产品!“南沙麒麟”正式亮相
LeetCode 6109. Number of people who know the secret
Memory leak of viewpager + recyclerview
解决 contents have differences only in line separators
Is it safe for golden sun to open an account? Can I open an account free of 5 in case?
Penetrate the whole intranet through socks agent
How to automatically install pythn third-party libraries
How to obtain the coordinates of the aircraft passing through both ends of the radar
vs2017 qt的各种坑
Reptile 01 basic principles of reptile
Tupu software digital twin | visual management system based on BIM Technology
基于can总线的A2L文件解析(3)
Is it safe for Apple mobile phone to speculate in stocks? Is it a fraud to get new debts?
SAP 特征 特性 说明
Use of websocket tool
Record eval() and no in pytoch_ grad()
吳恩達團隊2022機器學習課程,來啦
瀚升优品app翰林优商系统开发功能介绍
How to choose the most formal and safe external futures platform?