如何在 Kotlin 数据类中记录属性?

2022-09-02 12:58:56

我应该把 Javadoc 放在 Kotlin 数据类中属性的什么位置?

换句话说,如何在 Kotlin 中编写以下 Java 代码:

/**
 * Represents a person.
 */
public class Person {
    /**
     * First name. -- where to place this documentation in Kotlin?
     */
    private final String firstName;
    /**
     * Last name. -- where to place this documentation in Kotlin?
     */
    private final String lastName;

    // a lot of boilerplate Java code - getters, equals, hashCode, ...
}

在 Kotlin 中,它看起来像这样:

/**
 * Represents a person.
 */
data class Person(val firstName: String, val lastName: String)

但是在哪里放置属性的文档?


答案 1

文档中所述,您可以将标记用于此目的:@property

/**
 * Represents a person.
 * @property firstName The first name.
 * @property lastName The last name.
 */
data class Person(val firstName: String, val lastName: String)

或者,如果您在文档中没有太多关于它们的内容,只需在类的描述中提及属性名称:

/**
 * Represents a person, with the given [firstName] and [lastName].
 */
data class Person(val firstName: String, val lastName: String)

答案 2

推荐