JvmOverloads类主构造函数的注释

时间:2016-03-02 13:41:57

标签: constructor annotations kotlin

为什么禁止从类主构造函数自动生成Java可见的许多构造函数,默认params喜欢这个?

@JvmOverloads
class Video(private val id: Long, val ownerId: Long, var title: String? = null, var imgLink: String? = null, var videoLink: String? = null,
        var description: String? = null, var created: Date? = null, var accessKey: String? = null, var duration: Long? = null,
        var views: Long? = null, var comments: Long? = null) : Entity
  

此注释不适用于目标'

;

1 个答案:

答案 0 :(得分:45)

这不是禁止的,您只是将@JvmOverloads应用于错误的目标。注释主构造函数的正确方法是在constructor之前明确指定@JvmOverloads关键字:

class Video @JvmOverloads constructor(
    private val id: Long,
    val ownerId: Long,
    var title: String? = null,
    var imgLink: String? = null,
    var videoLink: String? = null,
    var description: String? = null,
    var created: Date? = null,
    var accessKey: String? = null,
    var duration: Long? = null,
    var views: Long? = null,
    var comments: Long? = null
) : Entity