1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| import cn.hutool.core.bean.BeanUtil; import lombok.Data;
@Data public class User { private String name; private Integer age; private String sex; private String email;
private String other;
public void appendOther(String other) { this.other = other; }
public static void main(String[] args) { User user1 = new User(); user1.setName("Jojo"); user1.setAge(18); user1.setSex("女"); user1.setEmail("JoJo@example.com"); user1.appendOther("其他数据"); System.out.println(user1);
User user2 = new User() {{ setName("Jojo"); setAge(18); setSex("女"); setEmail("JoJo@example.com"); appendOther("其他数据"); }}; System.out.println(user2);
final User user3 = SetBuilder.of(new User()) .set(User::setName, "JoJo") .set(User::setAge, 18) .set(User::setSex, "女") .set(User::setEmail, "JoJo@example.com") .set(User::appendOther, "其他数据") .build(); System.out.println(user3);
final User user4 = new User(); BeanUtil.setFieldValue(user4, "name", "JoJo"); BeanUtil.setFieldValue(user4, "age", 18); BeanUtil.setFieldValue(user4, "sex", "女"); BeanUtil.setFieldValue(user4, "email", "JoJo@example.com"); BeanUtil.setFieldValue(user4, "other", "其他数据"); System.out.println(user4); }
}
|