gpt4 book ai didi

java - 如何在方法的javadoc中显示变量的值?

转载 作者:行者123 更新时间:2023-11-29 04:52:31 25 4
gpt4 key购买 nike

我有一个构建器模式,其中有一个静态类,如下所示:

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 ..} 就像时钟的序列号。 “包装”时钟的人知道实际的序列号并写出来而不是 。

附言。完全希望这更符合您实际寻找的内容:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com