溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

@Builder注解如何在Java中使用

發(fā)布時(shí)間:2021-01-06 15:12:05 來(lái)源:億速云 閱讀:350 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

@Builder注解如何在Java中使用?相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。

Lombok中@Builder用法

1、建造者模式簡(jiǎn)介:Builder 使用創(chuàng)建者模式又叫建造者模式。簡(jiǎn)單來(lái)說(shuō),就是一步步創(chuàng)建一個(gè)對(duì)象,它對(duì)用戶屏蔽了里面構(gòu)建的細(xì)節(jié),但卻可以精細(xì)地控制對(duì)象的構(gòu)造過(guò)程。

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è)開(kāi)發(fā)中,一般在領(lǐng)域?qū)ο髮?shí)體上標(biāo)注@Builder,其作用就相當(dāng)于@AllArgsConstructor(access = AccessLevel.PRIVATE),@Builder一般與@Getter結(jié)合使用。

3、實(shí)戰(zhàn)
① 編寫測(cè)試實(shí)體類。

import lombok.Builder;
import lombok.Getter;

@Builder
//@Getter
public class Person {
  private String name;

  private String id;

  private String phoneNumeber;
}

② 編寫測(cè)試類。

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í)行的結(jié)果為:
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的作用:
生成一個(gè)全屬性的構(gòu)造器
生成了一個(gè)返回靜態(tài)內(nèi)部類PersonBuilder對(duì)象的方法
生成了一個(gè)靜態(tài)內(nèi)部類PersonBuilder,這個(gè)靜態(tài)內(nèi)部類包含Person類的三個(gè)屬性,無(wú)參構(gòu)造器,三個(gè)方法名為屬性名的方法,返回Person對(duì)象的build方法,輸出靜態(tài)內(nèi)部類三個(gè)屬性的toString()方法。

⑤ 建造者使用過(guò)程:

 Person.PersonBuilder builder = Person.builder();
    builder.phoneNumeber("11111")
        .id("1123")
        .name("asdd").build();
    System.out.println(builder);

先實(shí)例化內(nèi)部類對(duì)象并返回,然后為調(diào)用內(nèi)部類的方法為內(nèi)部類的屬性賦值,build()方法就是將內(nèi)部類PersonBuilder的屬性值傳入Person構(gòu)造器中,實(shí)例化Person對(duì)象。

看完上述內(nèi)容,你們掌握@Builder注解如何在Java中使用的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI