这是官方文档中的一个例子(你需要向下滚动一点):</p>
class Person {
private final @Id Long id;
private final String firstname, lastname;
private final LocalDate birthday;
private final int age;
private String comment;
private @AccessType(Type.PROPERTY) String remarks;
static Person of(String firstname, String lastname, LocalDate birthday) {
return new Person(null, firstname, lastname, birthday,
Period.between(birthday, LocalDate.now()).getYears());
}
Person(Long id, String firstname, String lastname, LocalDate birthday, int age) {
this.id = id;
this.firstname = firstname;
this.lastname = lastname;
this.birthday = birthday;
this.age = age;
}
Person withId(Long id) {
return new Person(id, this.firstname, this.lastname, this.birthday);
}
void setRemarks(String remarks) {
this.remarks = remarks;
}
}
我对此有几个问题。
Person withId(Long id)
示例中不存在引用的构造函数吗?Person withId(Long id)
官方文档中有这样的描述:“相同的模式通常适用于存储管理的其他属性,但可能需要更改以进行持久化操作”。是不是可以这样理解:成功保存实例后,WithOutIdPerson
可以用于其他字段更改并再次保存,SavedPerson
还可以用于其他上层操作?为什么
of ()
在示例中调用工厂的最后一步?
谢谢~