尝试一些带有record
和记录组件的代码。我正在使用可变稀有组件,并在自定义构造函数上遇到编译时错误。
public record Break<R extends Record>(R record, String... notifications) {
public Break(R record, String... notifications) {
System.out.println("record: " + record + " and notifications: " + Arrays.toString(notifications));
this.record = record;
this.notifications = notifications;
}
// compile error: non canonical record constructor must delegate to another costructor
public Break(R record) {
System.out.println("record: " + record);
this.record = record;
}
public Break() {
this(null); // this works
// actually intelliJ suggests it uses the constructor that is not compiling
}
public static void main(String[] args) {
new Break<>(new Break<>());
}
}
我很想了解的是,在没有为初始化提供任何组件的情况下,当通过另一个自定义构造函数调用类似的构造函数时,如何推断出类似的构造函数。