【发布时间】:2016-07-21 05:16:42
【问题描述】:
我真的很喜欢有据可查的代码。但是文档中有很多重复之处,因为基本信息和示例应该可用于属性、setter/getter 和构造函数(另请参阅Simple Getter/Setter comments)。
有没有办法避免 JavaDocs 的重复?我唯一的想法是使用 {@link #function()} 并链接到 JavaDocs 中包含更多信息的部分。
这是一个虚构的例子:
package com.stackoverflow.tests;
/**
* An Event ...
*/
public class Event {
/**
* The date the event takes place.
* Needs to be in <a href="https://en.wikipedia.org/wiki/ISO_8601">ISO 8601</a> format.
*
* <h2>Examples:</h2>
* <ul>
* <li>{@code 2016-03-19}</li>
* <li>{@code 2016-03-19T05:54:01+00:00}</li>
* <li>{@code 2016-W11} - i.e. week 11 of 2016</li>
* </ul>
*/
private String date;
/**
* Creates an event initializing it with the date and location the event takes place.
* @param date
* Date in <a href="https://en.wikipedia.org/wiki/ISO_8601">ISO 8601</a> format. Examples:
* <ul>
* <li>{@code 2016-03-19}</li>
* <li>{@code 2016-03-19T05:54:01+00:00}</li>
* <li>{@code 2016-W11} - i.e. week 11 of 2016</li>
* </ul>
* @param location
* Location ...
*/
public Event(final String date, final String location) {
this.date = date;
}
/**
* The date the event takes place.
* @return
* Date in <a href="https://en.wikipedia.org/wiki/ISO_8601">ISO 8601</a> format.
*/
public String getDate() {
return date;
}
/**
* Updates the date the event takes place using an ISO 8601 formatted String.
* @param date
* Date in <a href="https://en.wikipedia.org/wiki/ISO_8601">ISO 8601</a> format. Examples:
* <ul>
* <li>{@code 2016-03-19}</li>
* <li>{@code 2016-03-19T05:54:01+00:00}</li>
* <li>{@code 2016-W11} - i.e. week 11 of 2016</li>
* </ul>
*/
public void setDate(String date) {
this.date = date;
}
}
【问题讨论】:
-
拥有自言自语名字的私有成员真的需要详细描述吗,尤其是在 bean 类中?
-
@SashaSalauyou:嗯,也许这是一个不好的例子。但总的来说,我认为将文档添加到私有成员变量是一个好主意。在处理代码时确实很有帮助(例如,Eclipse 将鼠标移到记录的方法上时显示文档)。
-
如果你担心这个,你的类必须有很多 getter/setter 对。 It can be argued 这表明你的班级设计很差。
标签: java documentation javadoc