当前位置:网站首页>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)
边栏推荐
- 隐私计算助力数据的安全流通与共享
- 使用Jmeter虚拟化table失败
- The easycvr platform reports an error "ID cannot be empty" through the interface editing channel. What is the reason?
- Use JMeter to record scripts and debug
- Pytorch yolov5 training custom data
- Sophon CE Community Edition is online, and free get is a lightweight, easy-to-use, efficient and intelligent data analysis tool
- 分享:中兴 远航 30 pro root 解锁BL magisk ZTE 7532N 8040N 9041N 刷机 刷面具原厂刷机包 root方法下载
- Star Ring Technology launched transwarp Navier, a data element circulation platform, to help enterprises achieve secure data circulation and collaboration under privacy protection
- 瞅一瞅JUC提供的限流工具Semaphore
- Sophon base 3.1 launched mlops function to provide wings for the operation of enterprise AI capabilities
猜你喜欢

图片数据不够?我做了一个免费的图像增强软件

图扑软件数字孪生 | 基于 BIM 技术的可视化管理系统

记一次使用Windbg分析内存“泄漏”的案例

Isprs2022 / Cloud Detection: Cloud Detection with Boundary nets Boundary Networks Based Cloud Detection

@Extension, @spi annotation principle

To solve the stubborn problem of Lake + warehouse hybrid architecture, xinghuan Technology launched an independent and controllable cloud native Lake warehouse integrated platform

LeetCode 6109. Number of people who know the secret

ConvMAE(2022-05)

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

U-Net: Convolutional Networks for Biomedical Images Segmentation
随机推荐
New words new words new words new words [2]
About statistical power
苹果手机炒股安全吗?打新债是骗局吗?
基于can总线的A2L文件解析(3)
Le cours d'apprentissage de la machine 2022 de l'équipe Wunda arrive.
英语句式参考
ViewPager + RecyclerView的内存泄漏
ClickHouse(03)ClickHouse怎么安装和部署
《2022中国信创生态市场研究及选型评估报告》发布 华云数据入选信创IT基础设施主流厂商!
JVM third talk -- JVM performance tuning practice and high-frequency interview question record
【PaddlePaddle】 PaddleDetection 人脸识别 自定义数据集
MATLAB中print函数使用
[paddleclas] common commands
图扑软件数字孪生 | 基于 BIM 技术的可视化管理系统
Maximum artificial island [how to make all nodes of a connected component record the total number of nodes? + number the connected component]
OpenShift常用管理命令杂记
[use electron to develop desktop on youqilin]
Is it safe to open an account, register and dig money? Is there any risk? Is it reliable?
Writing writing writing
图片数据不够?我做了一个免费的图像增强软件