当前位置:网站首页>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)
边栏推荐
- Find the first k small element select_ k
- Sophon CE社区版上线,免费Get轻量易用、高效智能的数据分析工具
- Clickhouse (03) how to install and deploy Clickhouse
- 爬虫01-爬虫基本原理讲解
- [QNX Hypervisor 2.2用户手册]6.3.2 配置VM
- How can cluster deployment solve the needs of massive video access and large concurrency?
- [paddlepaddle] paddedetection face recognition custom data set
- 【PaddleClas】常用命令
- Introduction to the development function of Hanlin Youshang system of Hansheng Youpin app
- 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
猜你喜欢
随机推荐
Isprs2022 / Cloud Detection: Cloud Detection with Boundary nets Boundary Networks Based Cloud Detection
金太阳开户安全吗?万一免5开户能办理吗?
【HCIA-cloud】【1】云计算的定义、什么是云计算、云计算的架构与技术说明、华为云计算产品、华为内存DDR配置工具说明
在通达信上做基金定投安全吗?
文章中的逻辑词
星环科技重磅推出数据要素流通平台Transwarp Navier,助力企业实现隐私保护下的数据安全流通与协作
Use of print function in MATLAB
《2022中国信创生态市场研究及选型评估报告》发布 华云数据入选信创IT基础设施主流厂商!
Copy the linked list with random pointer in the "Li Kou brush question plan"
LeetCode 6109. 知道秘密的人数
苹果手机炒股安全吗?打新债是骗局吗?
How to solve the error "press any to exit" when deploying multiple easycvr on one server?
[PM2 details]
Xiaobai getting started with NAS - quick building private cloud tutorial series (I) [easy to understand]
Star Ring Technology launched transwarp Navier, a data element circulation platform, to help enterprises achieve secure data circulation and collaboration under privacy protection
OpenShift常用管理命令杂记
【在优麒麟上使用Electron开发桌面应】
案例分享|金融业数据运营运维一体化建设
@Extension、@SPI注解原理
buuctf-pwn write-ups (9)









