使用新的 Java 14 Record 功能,是否可以为同一个 Record 创建多个构造函数?
With the new Java 14 Record functionality, is it possible of creating multiples constructors for the same Record?
我有一堆使用 Lombok 的“数据”类,我想迁移所有这些数据以使用 Java 14 中可用的新记录功能。使用新的 Java 14 记录功能,是否可以为同一个记录创建多个构造函数?如果没有,是否有替代方案?
对于Java14,记录不能有多个构造函数(参考:Java 14 - JEP 359: Records (Preview))。
从 Java 15 和 16+ 开始,记录可能有多个构造函数。
(参见 Java 15 - JEP 384: Records (Second Preview) and Java 16 - JEP 395: Records (Final))。
但是,每个构造函数 必须委托给记录的规范构造函数 ,它可以显式定义或自动生成。
一个例子:
public record Person(
String firstName,
String lastName
) {
// Compact canonical constructor:
public Person {
// Validations only; fields are assigned automatically.
Objects.requireNonNull(firstName);
Objects.requireNonNull(lastName);
// An explicit fields assignment, like
// this.firstName = firstName;
// would be a syntax error in compact-form canonical constructor
}
public Person(String lastName) {
// Additional constructors MUST delegate to the canonical constructor,
// either directly:
this("John", lastName);
}
public Person() {
// ... or indirectly:
this("Doe");
}
}
另一个例子:
public record Person(
String firstName,
String lastName
) {
// Canonical constructor isn't defined in code,
// so it is generated implicitly by the compiler.
public Person(String lastName) {
// Additional constructors still MUST delegate to the canonical constructor!
// This works:
this("John", lastName);
// (Re-)Assigning fields here directly would be a compiler error:
// this.lastName = lastName; // ERROR: Variable 'lastName' might already have been assigned to
}
public Person() {
// Delegates to Person(String), which in turn delegates to the canonical constructor:
this("Doe");
}
}
我有一堆使用 Lombok 的“数据”类,我想迁移所有这些数据以使用 Java 14 中可用的新记录功能。使用新的 Java 14 记录功能,是否可以为同一个记录创建多个构造函数?如果没有,是否有替代方案?
对于Java14,记录不能有多个构造函数(参考:Java 14 - JEP 359: Records (Preview))。
从 Java 15 和 16+ 开始,记录可能有多个构造函数。 (参见 Java 15 - JEP 384: Records (Second Preview) and Java 16 - JEP 395: Records (Final))。
但是,每个构造函数 必须委托给记录的规范构造函数 ,它可以显式定义或自动生成。
一个例子:
public record Person(
String firstName,
String lastName
) {
// Compact canonical constructor:
public Person {
// Validations only; fields are assigned automatically.
Objects.requireNonNull(firstName);
Objects.requireNonNull(lastName);
// An explicit fields assignment, like
// this.firstName = firstName;
// would be a syntax error in compact-form canonical constructor
}
public Person(String lastName) {
// Additional constructors MUST delegate to the canonical constructor,
// either directly:
this("John", lastName);
}
public Person() {
// ... or indirectly:
this("Doe");
}
}
另一个例子:
public record Person(
String firstName,
String lastName
) {
// Canonical constructor isn't defined in code,
// so it is generated implicitly by the compiler.
public Person(String lastName) {
// Additional constructors still MUST delegate to the canonical constructor!
// This works:
this("John", lastName);
// (Re-)Assigning fields here directly would be a compiler error:
// this.lastName = lastName; // ERROR: Variable 'lastName' might already have been assigned to
}
public Person() {
// Delegates to Person(String), which in turn delegates to the canonical constructor:
this("Doe");
}
}