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

时间:2018-04-05 14:31:04

标签: java kotlin javadoc kdoc

我应该在哪里将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 个答案:

答案 0 :(得分:10)

documentation所述,您可以使用@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)