本文介绍了类主构造函数的 JvmOverloads 注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么禁止从类主构造函数自动生成许多对 Java 可见的构造函数,默认参数像这样?

@JvmOverloads类视频(私有 val id: Long, val ownerId: Long, var title: String? = null, var imgLink: String? = null, var videoLink: String? = null,变量描述:字符串?= null, var created: 日期?= null, var accessKey: 字符串?= null,var 持续时间:长?= 空,var 意见:长?= null, var 评论:长?= null) : 实体

此注解不适用于目标'class'

解决方案

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

class Video @JvmOverloads 构造函数(私人有效 ID:长,val ownerId:长,var 标题:字符串?= 空,var imgLink:字符串?= 空,var videoLink:字符串?= 空,变量描述:字符串?= 空,变量创建:日期?= 空,var accessKey:字符串?= 空,var 持续时间:长?= 空,var 意见:长?= 空,var 评论:长?= 空) : 实体

Why is it prohibited to autogenerate many constructors visible to Java from class primary constructor with default params likes this?

@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
解决方案

It's not prohibited, you are just applying @JvmOverloads to the wrong target. The proper way to annotate primary constructor is to explicitly specify constructor keyword with @JvmOverloads before:

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

这篇关于类主构造函数的 JvmOverloads 注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 00:06