Kotlin学习笔记——扩展

    Kotlin 同 C# 类似,能够无需继承该类或使用像装饰者这样的任何类型的设计模式,就能扩展一个类的新功能。 完成这种功能的特殊方法叫做 扩展 。Kotlin 支持 扩展函数扩展属性

扩展函数

    声明一个扩展函数,需要用一个 接收者类型, 也就是被扩展的类型来作为他的前缀。

  • 以下示例代码是为String类添加一个扩展函数,判断是否有效
fun String.isValidPwd(): Boolean {
    return this.isNotEmpty() && this.length < 20
}

这个 this 关键字在扩展函数内部对应到接收者对象(传过来的在点符号前的对象)

  • 扩展函数支持泛型
  • 扩展不能真正的修改所扩展的类,并没有在一个类中插入新成员, 仅仅是可以通过该类型的变量用点表达式去调用这个新函数。
  • 扩展是静态解析的,他只会根据定义的类型进行接卸调用,即他们不是根据接收者类型的虚方法。
open class A

class B: A()

fun A.printMsg() = "This is A print"

fun B.printMsg() = "This is B print"

fun printMsg(a: A) {
    println(a.printMsg())
}
fun main(args: Array<String>) {

    printMsg(B())
}

以上示例中,printMsg()方法定义时参数类型是A,实际调用是传入子类B的对象,但是实际输出却是调用了A的扩展方法。

  • 如果扩展函数与类的成员函数具有相同的签名,调用时总是调用成员函数。
open class A

class B: A() {
    fun printMsg() = "This is inside B print"
}

fun A.printMsg() = "This is A print"

fun B.printMsg() = "This is B print"

fun main(args: Array<String>) {
    println(B().printMsg())
}

以上示例中,扩展函数与类的成员函数具有相同的签名,调用时总是调用成员函数。

扩展属性

跟扩展函数一样,Kotlin中支持扩展属性。

  • 因扩展属性并没有在类中插入属性,所以幕后字段是无效的。
  • 扩展字段不能有初始化器(原因如上所述),只能提供显式的getter/setter
class B {
}

val B.desc: String
get() {
    return "This is a desc"
}

val B.test: String = "This is a test" // 扩展字段不能有初始化器,所以这样定义是错误的

扩展的作用域

  • 一般在顶层(及包内)定义扩展,将会在整个包内可以访问
  • 如果需要访问其他包定义的扩展,可以通过引入包的方式调用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值