此代码使用Java并使用Dropwizard和Lombok。我想知道是否有一个我可以使用的注释或一些其他方法来使一个类的数据成员存在,如果它有一些非空值或如果值为null则不存在。 我不确定这是否是可能的,但我试图在一些限制下工作。
相关的VerificationResponse类是:
private SomeResponseContext context;
public static VerificationResponse response(String responseCode, String responseType,
String explanationCode, String explanation, String optionalField) {
SomeResponseContext context = VerificationResponseContext.builder().responseCode(responseCode).
responseType(responseType).explanationCode(explanationCode).explanation(explanation).optionalField(optionalField).build();
VerificationResponse verifyResponse = VerificationResponse.builder().context(context).build();
return verifyResponse;
}
someResponseContext类如下所示:
@Getter
@Setter
@Builder(builderClassName="SomeResponseContextBuilder")
@NoArgsConstructor
@AllArgsConstructor
@ToString
@ ? //Don't include optionalField if it is null
public class SomeResponseContext {
private String responseCode;
private String responseType;
private String explanationCode;
private String explanation;
private String optionalField;
}
当我返回VerificationResponse verifyResponse时,我希望其SomeResponseContext成员拥有上面发布的SomeResponseContext代码等所有字段,或者,如果在构建SomeResponseContext时,如果Sting optionalField的值为null /不存在则该数据成员被删除,所有实用目的的课程都是这样的:
public class SomeResponseContext {
private String responseCode;
private String responseType;
private String explanationCode;
private String explanation;
}
问题是某些情况下,verifyReponse只有四个数据成员,如果还有更多,它会崩溃。但是,出现了另一种情况,即在某些情况下我们需要第五个数据成员。
答案 0 :(得分:1)
我认为不可能要求JVM加载类型/类 有两种不同的结构:
class foo{
String s1;
}
class foo{
String s1;
String s2;
}
使用哪一个?这是模棱两可的。如果额外的字段随着时间的推移变得昂贵,那么你最好的选择是使用抽象类
abstract class foo{
protected String s1;
}
class foo1 extends foo{
}
class foo2 extends foo{
String s2;
}
龙目岛应该能够选择那些