问题描述
我有一个班级
@Value
@NonFinal
public class A {
int x;
int y;
}
我有另一个班级B
@Value
public class B extends A {
int z;
}
lombok抛出错误,说无法找到A()构造函数,显式调用它我想要lombok做的是给类b注释,使它生成以下代码:
lombok is throwing error saying it cant find A() constructor, explicitly call it what i want lombok to do is to give annotation to class b such that it generates the following code:
public class B extends A {
int z;
public B( int x, int y, int z) {
super( x , y );
this.z = z;
}
}
我们在Lombok有注释吗?
Do we have an annotation to do that in Lombok?
推荐答案
这在龙目岛是不可能的。虽然这将是一个非常好的功能,但它需要解决方案才能找到超类的构造函数。在调用Lombok的那一刻,超类只能通过名称来识别。使用import语句和类路径来查找实际的类并非易事。在编译期间,你不能只使用反射来获取构造函数列表。
This is not possible in Lombok. Although it would be a really nice feature, it requires resolution to find the constructors of the super class. The super class is only known by name the moment Lombok gets invoked. Using the import statements and the classpath to find the actual class is not trivial. And during compilation you cannot just use reflection to get a list of constructors.
这不是完全不可能的,但结果使用 val中的分辨率
和 @ExtensionMethod
告诉我们这很难且容易出错。
It is not entirely impossible but the results using resolution in val
and @ExtensionMethod
have taught us that is it hard and error-prone.
披露:我是龙目岛开发人员。
Disclosure: I am a Lombok developer.
这篇关于如何在Lombok中调用超级构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!