lombok的@Accessors注解
@AllArgsConstructor @Data @NoArgsConstructor @Accessors(chain = true) @EqualsAndHashCode public class User { String name; String sex; String num; }
使用:
package com.example.springbootstarterhello.chain; public class TestChain { public static void main(String[] args) { User user = new User(); /* user.setSex("ede"); user.setNum("32323");*/ user.setName("ego").setNum("123").setSex("male"); System.out.println(user); } }
注意:
@Builder注解加到类上时程序找不到类的无参构造方法
解决办法如下
第一种方法:Student加连个注解@NoArgsConstructor和@AllArgsConstructor
@Data @Builder @NoArgsConstructor @AllArgsConstructor public class Student { private Long id; private String name; private String teacherName; private String className; private String schoolName; }
@EqualsAndHashCode(callSuper = false)及重写Equals和HashCode方法:
子类里面这么使用@EqualsAndHashCode(callSuper = false) ,不调用父类的属性,那么子类属性里面的相同的话,那hashcode的值就相同啦,所以代码里面的2个tv的equals方法的返回值是true