Kotlin @JvmOverloads

/**
 * 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)