美文网首页
Kotlin @JvmOverloads

Kotlin @JvmOverloads

作者: 贼噶人 | 来源:发表于2021-02-22 19:39 被阅读0次
/**
 * Instructs the Kotlin compiler to generate overloads for this function that substitute default parameter values.
 *
 * If a method has N parameters and M of which have default values, M overloads are generated: the first one
 * takes N-1 parameters (all but the last one that takes a default value), the second takes N-2 parameters, and so on.
 */
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.CONSTRUCTOR)
@Retention(AnnotationRetention.BINARY)
@MustBeDocumented
public actual annotation class JvmOverloads

意思就是他会给有默认参数的方法生成重载方法,在Java调用Kotlin时

@JvmOverloads fun func(name:String, age:Int = 0){
       println("name = $name age = $age")
   } // 如果不加@JvmOverloads 只会生产一个包含所有参数的方法,如果加上会生成一个重载方法只有name参数

相关文章

网友评论

      本文标题:Kotlin @JvmOverloads

      本文链接:https://www.haomeiwen.com/subject/ofbkfltx.html