我们正在使用AutoBeans创建用于RPC-Calls的Pojo对象。 Pojo建议使用默认值或其他类初始化的方法是什么?
例如
public interface SamplePojo {
// should default to 5
int getSampleProperty();
void setSampleProperty(int sampleProperty);
}
public interface ModelFactory extends AutoBeanFactory {
AutoBean<SamplePojo> getSamplePojo();
}
SamplePojo有一个int属性,我们总是希望默认为5。
答案 0 :(得分:1)
应将AutoBeans视为低级别,直接映射到JSON或从JSON映射。考虑到这一点,您不希望getSampleProperty()
5,您宁愿检测属性的特定值的缺失,并且使用 5 in那种情况。
因此,如果0
(int
的默认值)不是属性的可接受值,则只需“如果属性为0则使用5”。否则,将返回类型更改为Integer
,如果属性为null
,则使用5。
答案 1 :(得分:0)
这会有用吗?
public interface SamplePojo {
// should default to 5
int getSampleProperty();
void setSampleProperty(int sampleProperty);
}
public class SamplePojoImpl implements SamplePojo{
private int sampleProperty = 5
// getters/setters
int getSampleProperty(){ return sampleProperty;}
void setSampleProperty(int sampleProperty){this.sampleProperty = sampleProperty;}
}
public interface ModelFactory extends AutoBeanFactory {
AutoBean<SamplePojo> getSamplePojo(SamplePojoImpl samplePojo );
}