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

27
在Kotlin数据类的KDoc中,我应该把属性的Javadoc类比放在哪里?
换句话说,如何在Kotlin KDoc中编写以下Java代码的Javadoc:
/**
 * 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个回答

34
正如文档中所述,您可以使用@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)

4
可惜的是,当我在类前面输入/**时,IntelliJ Idea不能像对待方法参数那样为我生成框架结构。这是一个教育我们的失去的机会 :) - Honza Zidek
4
有意为之。请参见http://kotlinlang.org/docs/reference/coding-conventions.html#documentation-comments了解原因。 - yole
这是一个很好的观点。请考虑使用这些信息来增强你的回答,这是有教育意义的。我的意思是这是有意为之的,而且你已经涉及到了这个原因。 - Honza Zidek

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接