美文网首页禅与计算机程序设计艺术Kotlin专题Kotlin
Kotlin — Extension Functions(扩展函

Kotlin — Extension Functions(扩展函

作者: Kotyo | 来源:发表于2018-01-03 10:03 被阅读150次

扩展功能

扩展函数是帮助我们扩展类的功能,而不必修改原代码的函数。

换句话说,Kotlin中的扩展函数允许我们通过添加新的函数来扩展一个类的功能。

示例代码:

fun Int.triple():Int{
  return this*3
}

现在我们可以这样使用:

var result=3.triple()

接下来,我们介绍一下android中该如何使用:

fun ImageView.loadImage(url:String){
  GlideApp.with(context).load(url).into(this)
}

使用方法:

imageView.loadImage(url)

相关文章

网友评论

    本文标题:Kotlin — Extension Functions(扩展函

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