当前位置:网站首页>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)
边栏推荐
- 南京大学:新时代数字化人才培养方案探讨
- Introduction to Resampling
- 模拟百囚徒问题
- Failed to virtualize table with JMeter
- [use electron to develop desktop on youqilin]
- The 11th China cloud computing standards and Applications Conference | cloud computing national standards and white paper series release, and Huayun data fully participated in the preparation
- 最大人工岛[如何让一个连通分量的所有节点都记录总节点数?+给连通分量编号]
- Maximum artificial island [how to make all nodes of a connected component record the total number of nodes? + number the connected component]
- Tupu software digital twin | visual management system based on BIM Technology
- Sophon CE Community Edition is online, and free get is a lightweight, easy-to-use, efficient and intelligent data analysis tool
猜你喜欢

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!

Failed to virtualize table with JMeter

《ClickHouse原理解析与应用实践》读书笔记(5)
![Maximum artificial island [how to make all nodes of a connected component record the total number of nodes? + number the connected component]](/img/8b/a60fc36115580f018445e4c2a28a9d.png)
Maximum artificial island [how to make all nodes of a connected component record the total number of nodes? + number the connected component]

ConvMAE(2022-05)

第十届全球云计算大会 | 华云数据荣获“2013-2022十周年特别贡献奖”

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

U-Net: Convolutional Networks for Biomedical Images Segmentation

彻底理解为什么网络 I/O 会被阻塞?

Sophon autocv: help AI industrial production and realize visual intelligent perception
随机推荐
【PaddleClas】常用命令
How to solve the error "press any to exit" when deploying multiple easycvr on one server?
Tupu software digital twin | visual management system based on BIM Technology
Sophon CE Community Edition is online, and free get is a lightweight, easy-to-use, efficient and intelligent data analysis tool
buuctf-pwn write-ups (9)
模拟百囚徒问题
The 10th global Cloud Computing Conference | Huayun data won the "special contribution award for the 10th anniversary of 2013-2022"
Cronab log: how to record the output of my cron script
如何获取飞机穿过雷达两端的坐标
第十一届中国云计算标准和应用大会 | 华云数据成为全国信标委云计算标准工作组云迁移专题组副组长单位副组长单位
sample_rate(采样率),sample(采样),duration(时长)是什么关系
@Extension, @spi annotation principle
LeetCode 6111. 螺旋矩阵 IV
Share: ZTE Yuanhang 30 Pro root unlock BL magick ZTE 7532n 8040n 9041n brush mask original brush package root method Download
Le cours d'apprentissage de la machine 2022 de l'équipe Wunda arrive.
pytorch yolov5 训练自定义数据
Nanjing University: Discussion on the training program of digital talents in the new era
[utiliser Electron pour développer le Bureau sur youkirin devrait]
nano的CAN通信
Numerical calculation method chapter8 Numerical solutions of ordinary differential equations