【问题标题】:How to document attributes in Kotlin data class?如何在 Kotlin 数据类中记录属性?
【发布时间】:2018-09-15 10:17:38
【问题描述】:

Kotlin 数据类中属性的 Javadoc 应该放在哪里?

换句话说,如何在 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)

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

【问题讨论】:

    标签: java kotlin javadoc kdoc


    【解决方案1】:

    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)
    

    【讨论】:

    • 很遗憾,当我在类前面输入/** 时,IntelliJ Idea 生成骨架 - 与它执行的方式类似 i> 用于方法参数。这是一个教育我们的机会:)
    • 这是故意的。请参阅kotlinlang.org/docs/reference/… 了解原因。
    • 这是一个有效的观点。请考虑使用此信息增强您的答案,这具有教育意义。我的意思是这是故意的,为什么 - 你已经触及了这个原因。
    猜你喜欢
    • 2018-09-20
    • 2011-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多