我有一个构建器模式,其中有一个静态类,如下所示:
public final class DataKey {
private DataKey(Builder builder) {
}
public static class Builder {
protected int maxCount = 3;
// ..some other code here
/**
* This should be greater than 0 and less than equal to {@value Builder#maxCount}.
*
* @param count
* @throws IllegalArgumentException if count is less than 0 and greater than {@value Builder#maxCount}.
* @return Builder object
*/
public Builder addCount(int count) {
checkArgument(count > 0 && count < (maxCount + 1),
"maxCount should be greater than 0 and less than " + (maxCount + 1));
this.maxCount = count;
return this;
}
}
}
现在我想在我的 addCount
方法上添加一个 Javadoc,以便我可以在其上显示 maxCount
的值,而无需对实际数字进行硬编码。我尝试使用 {@value Builder#maxCount}
,当我查找该方法的 Javadoc 时,它没有显示值 3?我在这里做错了什么?
最佳答案
public static class Builder {
protected static final int DEFAULT_MAX_COUNT = 3;
/** count cannot be set to a value higher than this. By default, the value is {@value Builder#DEFAULT_MAX_COUNT}. */
protected int maxCount = DEFAULT_MAX_COUNT;
protected int count;
// ..some other code here
/**
* This should be greater than 0 and less than equal to {@link Builder#maxCount}. By default, {@value Builder#DEFAULT_MAX_COUNT}.
*
* @param count
* @throws IllegalArgumentException if count is less than 0 and greater than {@link Builder#maxCount}.
* @return Builder object
*/
public Builder setCount(int count) {
checkArgument(count > 0 && count < (maxCount + 1),
"count should be greater than 0 and less than " + (maxCount + 1));
this.count = count;
return this;
}
/** ... */
public Builder setMaxCount(int maxCount){
checkArgument(count > 0, "maxCount must be greater than 0");
this.maxCount = maxCount;
}
/** ... */
public int getMaxCount(int maxCount){
return this.maxCount;
}
}
IMO,在 Builder
中为 maxCount
设置 setter 和 getter 是没有意义的,而应该是一个常量 max (DEFAULT_MAX_COUNT
) .
javadoc 无法显示动态变量的值,因为文档在程序运行时不会更改。 javadoc 只是文本,没有程序正在运行,因此无法更新值。将其视为闹钟(程序)附带的手册(您的 javadoc)。该手册可以告诉您各种有用的事情以及如何使用时钟,但它不能告诉您闹钟设置为什么(因为它永远不会改变)。 {@value ..}
内容就像时钟的序列号。 “包装”时钟的人知道实际的序列号,并将其写出来而不是 。
ps。完全希望这更符合您实际寻找的内容:
public static class Builder {
protected static final int MAX_MAX_COUNT = 3;
/** By default, the value is {@value Builder#MAX_MAX_COUNT}. */
protected int maxCount = MAX_MAX_COUNT;
// ..some other code here
/**
* This should be greater than 0 and less than equal to {@value Builder#MAX_MAX_COUNT}.
*
* @param maxCount
* @throws IllegalArgumentException if maxCount is less than 0 and greater than {@link Builder#MAX_MAX_COUNT}.
* @return Builder object
*/
public Builder setMaxCount(int maxCount) {
checkArgument(maxCount > 0 && maxCount < (MAX_MAX_COUNT + 1),
"count should be greater than 0 and less than " + (MAX_MAX_COUNT + 1));
this.maxCount = maxCount;
return this;
}
}
关于java - 如何在方法上显示 javadoc 中变量的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34889680/