标签:system 控制 tostring chain pre cep required 参数 string
使用Lombok实现链式调用
1.静态调用
User对象: 对象中必须有一个值不为空staticname作为指定的参数并调用对象
@Accessors(chain = true) @Getter @Setter @RequiredArgsConstructor(staticName = "of") @ToString public class User { private Integer id; @NonNull private String username; private String password; private String remarks; private String true_name; }
main调用:
public static void main(String[] args) throws IOException { User user = User.of("张三") .setPassword("zhangsan123") .setTrue_name("zhangsaniii"); System.out.println(user);}
控制台输出:
User(id=null, username=张三, password=zhangsan123, remarks=null, true_name=zhangsaniii)
2.第二张创建方式
user对象
import lombok.*; @Data @Builder//链式调用<-- @RequiredArgsConstructor @AllArgsConstructor @ToString public class User { private Integer id; private String username; private String password; private String remarks; private String true_name; }
测试main
User build = User.builder(). username("zhangsan").id(2). password("123").build(); System.out.println(build);
输出:
User(id=2, username=zhangsan, password=123, remarks=null, true_name=null)
标签:system 控制 tostring chain pre cep required 参数 string
原文地址:https://www.cnblogs.com/q1359720840/p/10496439.html