More specific constraints in derived classes using JSR-303
Lets say there's a class Money
that has fields开发者_开发知识库 amount and currency:
class Money {
private int amount;
@NotNull Currency currency;
}
While Price
is also money it introduces additional constraint: amount cannot be negative
class Price extends Money {
// add more specific constraint for amount field: @Min(0)
}
Is it possible to express this using JSR-303?
Introducing a property level constraint should do the job:
class Price extends Money {
@Min(0)
int getAmount() {
return super.getAmount();
}
}
精彩评论