@Builder注解如何在Java中使用?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
創(chuàng)新互聯(lián)公司是一家以成都網(wǎng)站建設公司、網(wǎng)頁設計、品牌設計、軟件運維、seo優(yōu)化排名、小程序App開發(fā)等移動開發(fā)為一體互聯(lián)網(wǎng)公司。已累計為隧道混凝土攪拌車等眾行業(yè)中小客戶提供優(yōu)質的互聯(lián)網(wǎng)建站和軟件開發(fā)服務。1、建造者模式簡介:Builder 使用創(chuàng)建者模式又叫建造者模式。簡單來說,就是一步步創(chuàng)建一個對象,它對用戶屏蔽了里面構建的細節(jié),但卻可以精細地控制對象的構造過程。
2、注解類Builder.java注釋:
* The builder annotation creates a so-called 'builder' aspect to the class that is annotated or the class
* that contains a member which is annotated with {@code @Builder}.
* <p>
* If a member is annotated, it must be either a constructor or a method. If a class is annotated,
* then a private constructor is generated with all fields as arguments
* (as if {@code @AllArgsConstructor(access = AccessLevel.PRIVATE)} is present
* on the class), and it is as if this constructor has been annotated with {@code @Builder} instead.
* Note that this constructor is only generated if you haven't written any constructors and also haven't
* added any explicit {@code @XArgsConstructor} annotations. In those cases, lombok will assume an all-args
* constructor is present and generate code that uses it; this means you'd get a compiler error if this
* constructor is not present.
在企業(yè)開發(fā)中,一般在領域對象實體上標注@Builder,其作用就相當于@AllArgsConstructor(access = AccessLevel.PRIVATE),@Builder一般與@Getter結合使用。
3、實戰(zhàn)
① 編寫測試實體類。
import lombok.Builder; import lombok.Getter; @Builder //@Getter public class Person { private String name; private String id; private String phoneNumeber; }
② 編寫測試類。
public class Test { public static void main(String[] args) { Person.PersonBuilder builder = Person.builder(); builder.phoneNumeber("11111") .id("1123") .name("asdd").build(); System.out.println(builder); } }
③編譯并執(zhí)行的結果為:
Person.PersonBuilder(name=asdd, id=1123, phoneNumeber=11111)
④ 編譯后的字節(jié)碼分析:
// // Source code recreated from a .class file by IntelliJ IDEA // (powered by Fernflower decompiler) // package com.atyunniao; public class Person { private String name; private String id; private String phoneNumeber; Person(String name, String id, String phoneNumeber) { this.name = name; this.id = id; this.phoneNumeber = phoneNumeber; } public static Person.PersonBuilder builder() { return new Person.PersonBuilder(); } public String getName() { return this.name; } public String getId() { return this.id; } public String getPhoneNumeber() { return this.phoneNumeber; } public static class PersonBuilder { private String name; private String id; private String phoneNumeber; PersonBuilder() { } public Person.PersonBuilder name(String name) { this.name = name; return this; } public Person.PersonBuilder id(String id) { this.id = id; return this; } public Person.PersonBuilder phoneNumeber(String phoneNumeber) { this.phoneNumeber = phoneNumeber; return this; } public Person build() { return new Person(this.name, this.id, this.phoneNumeber); } public String toString() { return "Person.PersonBuilder(name=" + this.name + ", id=" + this.id + ", phoneNumeber=" + this.phoneNumeber + ")"; } } }
@Builder的作用:
生成一個全屬性的構造器
生成了一個返回靜態(tài)內部類PersonBuilder對象的方法
生成了一個靜態(tài)內部類PersonBuilder,這個靜態(tài)內部類包含Person類的三個屬性,無參構造器,三個方法名為屬性名的方法,返回Person對象的build方法,輸出靜態(tài)內部類三個屬性的toString()方法。
⑤ 建造者使用過程:
Person.PersonBuilder builder = Person.builder(); builder.phoneNumeber("11111") .id("1123") .name("asdd").build(); System.out.println(builder);
先實例化內部類對象并返回,然后為調用內部類的方法為內部類的屬性賦值,build()方法就是將內部類PersonBuilder的屬性值傳入Person構造器中,實例化Person對象。
看完上述內容,你們掌握@Builder注解如何在Java中使用的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
分享文章:@Builder注解如何在Java中使用-創(chuàng)新互聯(lián)
鏈接URL:http://jinyejixie.com/article16/egcgg.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供外貿建站、移動網(wǎng)站建設、軟件開發(fā)、企業(yè)建站、手機網(wǎng)站建設、建站公司
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內容