本篇文章會講到:@Accessors、@Buinder 註解使用。
1、@Accessors使用
- @Accessors官方文檔地址
- 注註解在屬性(類)上,配置getter和setter方法的生成結果,分別有三個屬性:fluent、chain、prefix。
- fluent的中文含義是流暢的,設置爲true,則getter和setter方法的方法名都是基礎屬性名,且setter方法返回當前對象。
- chain的中文含義是鏈式的,設置爲true,則setter方法返回當前對象。
- prefix的中文含義是前綴,用於生成getter和setter方法的字段名會忽視指定前綴(遵守駝峯命名)。
import lombok.Data;
import lombok.experimental.Accessors;
@Data
@Accessors(chain = true)
public class Example {
private String name;
private String phone;
private int age;
}
編譯後:
public class Example {
private String name;
private String phone;
private int age;
public String getName() {
return name;
}
public Example setName(String name) {
this.name = name;
return this;
}
public String getPhone() {
return phone;
}
public Example setPhone(String phone) {
this.phone = phone;
return this;
}
public int getAge() {
return age;
}
public Example setAge(int age) {
this.age = age;
return this;
}
}
驗證結果:
Example example = new Example()
.setName("piao")
.setAge(24)
.setPhone("138");
2、@Buinder使用
- @Buinder官方文檔地址
- builder 是現在比較推崇的一種構建值對象的方式。該描述符用於將類改造成 builder(建造者)模式,用在類、方法或者構造函數上。
import lombok.Builder;
import lombok.Singular;
import java.util.Set;
@Builder
public class BuilderExample {
@Builder.Default private long created = System.currentTimeMillis();
private String name;
private int age;
@Singular private Set<String> occupations;
}
編譯後:
import java.util.Set;
public class BuilderExample {
private long created;
private String name;
private int age;
private Set<String> occupations;
BuilderExample(String name, int age, Set<String> occupations) {
this.name = name;
this.age = age;
this.occupations = occupations;
}
private static long $default$created() {
return System.currentTimeMillis();
}
public static BuilderExampleBuilder builder() {
return new BuilderExampleBuilder();
}
public static class BuilderExampleBuilder {
private long created;
private boolean created$set;
private String name;
private int age;
private java.util.ArrayList<String> occupations;
BuilderExampleBuilder() {
}
public BuilderExampleBuilder created(long created) {
this.created = created;
this.created$set = true;
return this;
}
public BuilderExampleBuilder name(String name) {
this.name = name;
return this;
}
public BuilderExampleBuilder age(int age) {
this.age = age;
return this;
}
public BuilderExampleBuilder occupation(String occupation) {
if (this.occupations == null) {
this.occupations = new java.util.ArrayList<String>();
}
this.occupations.add(occupation);
return this;
}
public BuilderExampleBuilder occupations(Collection<? extends String> occupations) {
if (this.occupations == null) {
this.occupations = new java.util.ArrayList<String>();
}
this.occupations.addAll(occupations);
return this;
}
public BuilderExampleBuilder clearOccupations() {
if (this.occupations != null) {
this.occupations.clear();
}
return this;
}
public BuilderExample build() {
// complicated switch statement to produce a compact properly sized immutable set omitted.
Set<String> occupations = ...;
return new BuilderExample(created$set ? created : BuilderExample.$default$created(), name, age, occupations);
}
@java.lang.Override
public String toString() {
return "BuilderExample.BuilderExampleBuilder(created = " + this.created + ", name = " + this.name + ", age = " + this.age + ", occupations = " + this.occupations + ")";
}
}
}
驗證結果:
BuilderExample example = BuilderExample.builder()
.name("piao")
.age(24)
.occupation("ONE")
.occupation("TWO")
.build();