1

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

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?我在这里做错了什么?

4

2 回答 2

2
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;
    }

}

maxCountIMO,为in设置 setter 和 getter 是没有意义的,Builder而应该是一个常量最大值(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;
    }
}
于 2016-01-20T02:06:36.940 回答
1

您只能对常量执行此操作。当您将变量设为静态时,它将起作用。

于 2016-01-20T01:07:44.830 回答