开发者

More specific constraints in derived classes using JSR-303

开发者 https://www.devze.com 2023-04-07 09:52 出处:网络
Lets say there\'s a class Money that has fields开发者_开发知识库 amount and currency: class Money {

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

精彩评论

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

关注公众号