当前位置:网站首页>【学习笔记】lombok的@Builder注解
【学习笔记】lombok的@Builder注解
2022-07-27 15:58:00 【棉花糖灬】
首先需要了解下Builder模式:
定义一个User类:
public class User {
private String firstName;
private String lastName;
User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public static Builder builder() {
return new Builder();
}
public static class Builder {
String firstName;
String lastName;
Builder firstName(String value) {
this.firstName = value;
return this;
}
Builder lastName(String value) {
this.lastName = value;
return this;
}
public User build() {
return new User(firstName, lastName);
}
}
}
则可以通过链式调用的方式为User的属性赋值:
User user = User.builder()
.firstName("san")
.lastName("zhang")
.build();
之所以能够链式调用是因为User.Builder类中的firstName()和lastName()方法返回的都是Builder对象。
而@Builder注解的使用可以大大简化代码:
@Builder
public class User {
private String firstName;
private String lastName;
}
在使用时和之前一样:
User user = User.Build()
.firstName("san")
.lastName("zhang")
.build();
边栏推荐
- 年终总结模板
- Likeshop takeout ordering system "100% open source without encryption"
- I got the P8 "top-level" distributed architecture manual crazy spread on Alibaba intranet
- Oracle 11g database installation tutorial
- Salesforce dynamic dashboard dynamic reports, limitations and Solutions
- Introduction to ef framework
- The concept and characteristics of network security grid are simple and popular
- Behind every piece of information you collect, you can't live without TA
- Count the six weapons of the domestic interface cooperation platform!
- Operation of simulated examination platform for 2022 low voltage electrician examination questions
猜你喜欢

Kubernetes 1.24 high availability cluster binary deployment

最新大厂高级面试题 必备

TCP的连接状态标识 (SYN, FIN, ACK, PSH, RST, URG)

Knowledge dry goods: basic storage service novice Experience Camp

2022 safety officer-a certificate examination questions and online simulation examination

What's the use of games| Game application value research case collection

WebDriverException( selenium.common.exceptions.WebDriverException: Message: ‘chromedriver‘ executabl

Fast analysis combined with Haidian medicine

Bug records using distributed framework WCF

细数国产接口协作平台的六把武器!
随机推荐
卷积神经网络——SSD论文翻译
Hutool digital computing
使用分布式框架WCF出现的BUG记录
Knowledge dry goods: basic storage service novice Experience Camp
Profiles vs Permission Sets
Could not obtain transaction-synchronized Session for current thread
6月第1周易盾业务风控关注 | 新东方学而思等15家机构被顶格罚款
Understand JVM language
SQL Server连接到服务器无效的解决办法
【Codeforces】 B. Make it Divisible by 25
用slmgr命令激活正版Win7旗舰版系统
2022 high altitude installation, maintenance and removal of test question simulation test platform operation
@Scheduled 和Quartz
X-sheet development tutorial: initialization configuration custom layout
Understand │ what is cross domain? How to solve cross domain problems?
卷积神经网络之卷积计算过程个人理解
Introduction to ef framework
微信小程序 实现拨打电话
面试常见问题一二
查找表中多余重复记录并删除保留最小一个