我有一节课
@Value
@NonFinal
public class A {
int x;
int y;
}我还有另一个B班
@Value
public class B extends A {
int z;
}lombok抛出错误,说它找不到A()构造函数,显式地调用它,我希望lombok做的是给类b添加注释,以便它生成以下代码:
public class B extends A {
int z;
public B( int x, int y, int z) {
super( x , y );
this.z = z;
}
}我们在Lombok中有注释可以做到这一点吗?
发布于 2020-08-03 21:49:44
作为一种选择,您可以使用com.fasterxml.jackson.databind.ObjectMapper从父类初始化子类
public class A {
int x;
int y;
}
public class B extends A {
int z;
}
ObjectMapper MAPPER = new ObjectMapper(); //it's configurable
MAPPER.configure( DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false );
MAPPER.configure( SerializationFeature.FAIL_ON_EMPTY_BEANS, false );
//Then wherever you need to initialize child from parent:
A parent = new A(x, y);
B child = MAPPER.convertValue( parent, B.class);
child.setZ(z);如果需要,您仍然可以在A和B上使用任何lombok注释。
https://stackoverflow.com/questions/29740078
复制相似问题