public class UserInfo { private String name; private String zhName; private String account; private String password; public UserInfo(String name, String zhName, String account, String password) { this.name = name; this.zhName = zhName; this.account = account; this.password = password; } public UserInfo() { } public static class Builder{ private String name; private String zhName; private String account; private String password; public Builder name(String name){ this.name = name; return this; } public Builder zhName(String zhName){ this.zhName = zhName; return this; } public Builder account(String account){ this.account = account; return this; } public Builder password(String password){ this.password = password; return this; } public UserInfo build(){ return new UserInfo(this.name,this.zhName,this.account,this.password); } } public static Builder builder() { return new Builder(); } @Override public String toString() { return "UserInfo{" + "name='" + name + '\'' + ", zhName='" + zhName + '\'' + ", account='" + account + '\'' + ", password='" + password + '\'' + '}'; } }
public static void main(String[] args) { UserInfo userInfo=UserInfo.builder() .name("小明") .zhName("夏明") .account("xxx@qw.com") .password("asdqwer") .build(); System.out.println(userInfo); }
UserInfo{name='小明', zhName='夏明', account='xxx@qw.com', password='asdqwer'}