Lombok中的@Builder注解怎么用
发表于:2025-02-04 作者:千家信息网编辑
千家信息网最后更新 2025年02月04日,这篇文章主要介绍Lombok中的@Builder注解怎么用,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!Lombok中的@Builder注解的使用作用@Builder注解的作用
千家信息网最后更新 2025年02月04日Lombok中的@Builder注解怎么用
这篇文章主要介绍Lombok中的@Builder注解怎么用,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
Lombok中的@Builder注解的使用
作用
@Builder注解的作用主要是用来生成对象,并且可以为对象链式赋值。
引入依赖
因为@Builder注解是lombok中的东西,所以第一步我们需要引入lombok的依赖,如下图:
第二步给实体类加上@Builder注解
第二步我们需要给我们的实体类加上一个@Builder注解,如下图:
第三步使用测试使用@Builder注解生成对象
实体类加上@Builder注解之后的编译结果
实体类加上@Builder注解之后,编译之后会多出一个builder()方法,和一个CardBuilder静态内部类,如下图:
代码如下:
public class Card { private int id; private String name; private boolean sex; public static Card.CardBuilder builder() { return new Card.CardBuilder(); } public Card(int id, String name, boolean sex) { this.id = id; this.name = name; this.sex = sex; } public Card() { } public int getId() { return this.id; } public String getName() { return this.name; } public boolean isSex() { return this.sex; } public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } public void setSex(boolean sex) { this.sex = sex; } public boolean equals(Object o) { if (o == this) { return true; } else if (!(o instanceof Card)) { return false; } else { Card other = (Card)o; if (!other.canEqual(this)) { return false; } else if (this.getId() != other.getId()) { return false; } else { Object this$name = this.getName(); Object other$name = other.getName(); if (this$name == null) { if (other$name == null) { return this.isSex() == other.isSex(); } } else if (this$name.equals(other$name)) { return this.isSex() == other.isSex(); } return false; } } } protected boolean canEqual(Object other) { return other instanceof Card; } public int hashCode() { int PRIME = true; int result = 1; int result = result * 59 + this.getId(); Object $name = this.getName(); result = result * 59 + ($name == null ? 43 : $name.hashCode()); result = result * 59 + (this.isSex() ? 79 : 97); return result; } public String toString() { return "Card(id=" + this.getId() + ", name=" + this.getName() + ", sex=" + this.isSex() + ")"; } 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 + ")"; } }}
以上是"Lombok中的@Builder注解怎么用"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!
注解
实体
对象
作用
内容
篇文章
生成
编译
东西
代码
价值
兴趣
小伙
小伙伴
方法
更多
知识
结果
行业
资讯
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
如何注意网络安全知识
兴化领风网络技术
悉尼 墨尔本 软件开发
医院网络安全小组职能科室
无线网络技术教程第二版答案
丰南区辅助网络技术售后保障
oracle数据库表的种类
网络技术员办公室
武汉众信天成网络技术公司
免费下载手机服务器
什么云服务器好用又安全
私营企业网络安全教育培训
我的世界破解版服务器地址
镇江智能化服务器厂商
数据库和数据表的关系
服务器漏洞类型
金山区创新软件开发好处
前端页面静态数据库
网络技术中的二八定律
浦东新区项目数据库销售价格
吃鸡游戏如何更改服务器
美国网络安全市场容量
服务器动态密码管理系统
ar眼镜软件开发
广州商城软件开发费用是多少
都匀网络安全系统怎么收费
部队网络安全黑板报图片
昌吉网络安全教育基地
网络安全认证的意义
数据库中怎么计算学分总和