前言
JDK25中的super,灵活构造函数体(JEP 513),在JDK25转正了,也就是允许在super前面添加代码
JDK25中的super
scala
public class Employee extends Person {
final String name;
Employee(String name, int age) {
if (age < 18 || age > 67) {
throw new IllegalArgumentException("参数异常");
}
super(age);
this.name = name;
}
static void main(String[] args) {
var emp = new Employee("aa", 20);
System.out.println("Person age set: " + emp.age);
}
}
class Person {
final int age;
Person(int age) {
this.age = age;
}
}
总结
JEP 513 允许在构造函数调用前执行代码,可以提高参数校验以及代码刻度