假设我有一个包含三个构造函数的类,如下所示:
public class ExampleClass {
// constructor #1
public ExampleClass(int a) {
this(a, "aaa"); // "aaa" is just an arbitrary default for b
}
// constructor #2
public ExampleClass(String b) {
this(2, b); // 2 is just an arbitrary default for a
}
// constructor #3
public ExampleClass(int a, String b) {
// a has an arbitrary minimum value of 3
// b has an arbitrary minimum length of 3
if (a < 2 || b.length() < 2) {
throw new IllegalArgumentException("a and b cannot be less than 2");
}
// ...
}
}
在这种情况下,我有三个构造函数。其中第三个是主构造函数,其他的只是提供默认值,因此可以只使用一个值而不是两个值来构造类。我正在尝试通过 JavaDoc 为此类类编写文档。如果我要为第一个或第二个构造函数编写文档,我会使用@throws
标签来记录潜力IllegalArgumentException
吗?或者我应该IllegalArgumentException
只为第三个构造函数的文档保存文档吗?a
在这种情况下,必须大于 2 或b
必须长于 2 个字符的最佳、最合适的表达方式是什么?我如何声明IllegalArgumentException
可以在没有记录的情况下抛出an @throws
?