开发者

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();
    }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜