- Data
@Data // 相当于 @getter @setter @ToString @EqualsAndHashCode,但需要所有属性的值相同才是同一个对象
@EqualsAndHashCode(of = {"id"})
public class UserInfoData {
private Long id;
private String name;
private String phone;
private Date birthDay;
private String address;
}
- Accessors
@Accessors(
// chain = true, // 链式编程
fluent = true
)
public class UserInfoAccessors {
private Long id;
private String name;
private String phone;
private Date birthDay;
private String address;
}
-
测试
-
Builder
@Builder
public class User {
private Long id;
private String name;
private String phone;
private Date birthDay;
private String address;
}
- 查看编译后的类
public class User {
private Long id;
private String name;
private String phone;
private Date birthDay;
private String address;
User(Long id, String name, String phone, Date birthDay, String address) {
this.id = id;
this.name = name;
this.phone = phone;
this.birthDay = birthDay;
this.address = address;
}
public static User.UserBuilder builder() {
return new User.UserBuilder();
}
public static class UserBuilder {
private Long id;
private String name;
private String phone;
private Date birthDay;
private String address;
UserBuilder() {
}
public User.UserBuilder id(Long id) {
this.id = id;
return this;
}
public User.UserBuilder name(String name) {
this.name = name;
return this;
}
public User.UserBuilder phone(String phone) {
this.phone = phone;
return this;
}
public User.UserBuilder birthDay(Date birthDay) {
this.birthDay = birthDay;
return this;
}
public User.UserBuilder address(String address) {
this.address = address;
return this;
}
public User build() {
return new User(this.id, this.name, this.phone, this.birthDay, this.address);
}
public String toString() {
return "User.UserBuilder(id=" + this.id + ", name=" + this.name + ", phone=" + this.phone + ", birthDay=" + this.birthDay + ", address=" + this.address + ")";
}
}
}
- 测试
public class Test {
public static void main(String[] args) {
User.UserBuilder builder = User.builder();
User user = builder.id(1L).build();
System.out.print(user);
}
}
标签:String,private,public,phone,User,使用,Lombok,id
From: https://www.cnblogs.com/chniny/p/16748936.html