我有一个接收 ArrayList
路由的构造函数:
public Voo(ArrayList<Routes> routes, LocalDateTime dateTime, Duration duration) {
if (routes.isEmpty()) {
}
this.routes = routes;
this.dateTime = dateTime;
this.duration = duration;
this.status = Status.CONFIRMED;
}
如果收到的 routes
为空,如何防止构造函数返回 Voo
实例?
最佳答案
通常会抛出IllegalArgumentException
。例如
if (routes.isEmpty()) {
throw new IllegalArgumentException("routes should be non-empty");
}
关于java - 防止使用无效参数构造实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39339451/