聊聊Lombok中的@Builder注解使用教程
Lombok中的@Builder注解的使用
作用
@Builder注解的作用主要是用來(lái)生成對(duì)象,并且可以為對(duì)象鏈?zhǔn)劫x值。
引入依賴
因?yàn)锧Builder注解是lombok中的東西,所以第一步我們需要引入lombok的依賴,如下圖:

第二步給實(shí)體類加上@Builder注解
第二步我們需要給我們的實(shí)體類加上一個(gè)@Builder注解,如下圖:

第三步使用測(cè)試使用@Builder注解生成對(duì)象

實(shí)體類加上@Builder注解之后的編譯結(jié)果
實(shí)體類加上@Builder注解之后,編譯之后會(huì)多出一個(gè)builder()方法,和一個(gè)CardBuilder靜態(tài)內(nèi)部類,如下圖:


代碼如下:
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 + ")";
}
}
}
到此這篇關(guān)于Lombok中的@Builder注解的使用的文章就介紹到這了,更多相關(guān)Lombok @Builder注解內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中的Callable實(shí)現(xiàn)多線程詳解
這篇文章主要介紹了Java中的Callable實(shí)現(xiàn)多線程詳解,接口Callable中有一個(gè)call方法,其返回值類型為V,這是一個(gè)泛型,值得關(guān)注的是這個(gè)call方法有返回值,這意味著線程執(zhí)行完畢后可以將處理結(jié)果返回,需要的朋友可以參考下2023-08-08
IDEA生成項(xiàng)目后出現(xiàn)的iml和idea文件問(wèn)題
這篇文章主要介紹了IDEA生成項(xiàng)目后出現(xiàn)的iml和idea文件問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
Java基本知識(shí)點(diǎn)之變量和數(shù)據(jù)類型
這篇文章主要給大家介紹了關(guān)于Java基本知識(shí)點(diǎn)之變量和數(shù)據(jù)類型的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Spring Boot + Vue 前后端分離項(xiàng)目如何踢掉已登錄用戶
這篇文章主要介紹了Spring Boot + Vue 前后端分離項(xiàng)目如何踢掉已登錄用戶,需要的朋友可以參考下2020-05-05
SpringBoot+Redis+Lua防止IP重復(fù)防刷攻擊的方法
本文主要介紹了SpringBoot+Redis+Lua防止IP重復(fù)防刷攻擊的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12

