如何在 KDoc 中引用辅助构造函数?

Jan*_*ich 6 kotlin kdoc

我有一个带有辅助构造函数的 Kotlin 类,应该在特定场景中使用。现在这应该从第一个构造函数中正确记录下来,以便您可以立即看到它。

/**
* This is my class.
* 
* @constructor This is the primary constructor meant for Scenario A.
* If you are in Scenario B, please use [reference to secondary constructor] <- what do I put in here?
*/
class MyClass(val a: String) {

    constructor(b: Int) : this("$b abc")

}
Run Code Online (Sandbox Code Playgroud)

我不知道在用于引用成员/函数的方括号中放什么,但我觉得这应该是可能的。如果有人对此了解更多,那就太好了

Joa*_*nha 0

我找不到任何可能性来完全做你想做的事,但我确实找到了替代方案。因此,此处的 KDocs 文档:KDoc明确表示这@constructor是特定于主构造函数的,但没有提及多个构造函数。通过阅读论坛和创建的问题,我也发现了有关此问题的一些内容,但我在那里找不到任何内容。然而,我确实提出了一个并不完美的想法,但我想这是我们目前能找到的最好的想法:

/**
 * This is my class.
 *
 * @property a A String
 * @constructor This is the primary constructor meant for Scenario A.
 * If you are in Scenario B, please use constructor MyClass with an [Int] as input argument
 */
class MyClass(val a: String) {

    /**
     * This is the other constructor
     * @param b Integer
     */
    constructor(b: Int) : this("$b abc")

}
Run Code Online (Sandbox Code Playgroud)

这允许您至少在主构造函数上记录第二个构造函数的存在,然后开发人员可以检查它的文档,因为它确实有效。所以一切都被记录下来,只是还没有直接链接。至少在这里,总是可以实现对第二个或多个其他构造函数的认识。