id
, name
, 和price
在第二个片段中不是字段,它们是记录组件。Yasin的回答已经提到了如何实现它,但只是为了完整起见,这里是你如何做到的:
/**
* A very complex Product description.
*
* @param id Product identifier; appears in "Record Components".
* @param name Product name appears; in "Record Components".
* @param price Product price; appears in "Record Components".
*/
public record Product(int id, String name, double price){}
标准 doclet 将忽略以下内容:
public record Product(
/**
* This comment would be ignored.
*/
int id,
/*
* Ignored
*/
String name,
// Ignored
double price) {}
如果您有一个字段,那么您可以向其中添加 Javadoc:
public record Product(...) {
/**
* Max Product price, appears in "Field Summary".
* <p>
* Appears in "Field Details".
*/
public static final double MAX_PRICE = Integer.MAX_VALUE >> 2;
}
要将 Javadoc 添加到规范构造函数中,可以使用紧凑样式:
public record Product(...) {
/**
* Product's compact canonical constructor, appears in "Constructor Summary".
* <p>
* In "Constructor Details".
*
* @param id Product identifier, appears in "Constructor Details"
* not in "Record Components".
* @param name Product name, appears in "Constructor Details"
* not in "Record Components".
* @param price Product price, appears in "Constructor Details"
* not in "Record Components".
* @throws IllegalArgumentException Allowed price range
* ({@code 0.0D}, {@link Product#MAX_PRICE}]
*/
public Product {
if (price <= 0 || price > MAX_PRICE) throw new IllegalArgumentException();
}
}