Kotlin教程:反射(Reflection)

2020-04-2311:27:20编程语言入门到精通Comments1,641 views字数 1627阅读模式

反射(Reflection)是一组语言和库特性,用于在运行时检查程序的结构。 Kotlin将函数和属性作为语言中的一等公民,并在运行时检查这些函数和属性。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18294.html

类引用

类引用用于获取KClass对象的引用。 为了获得静态Kclass的引用,应该使用类文字(即使用双冒号)。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18294.html

类引用的语法:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18294.html

val c1 = String::class  
val c2 = MyClass::class
Kotlin

参考值是KClass的类类型。 KClass类引用与Java类引用不同。 通过在KClass实例上使用.java属性来获取Java类引用。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18294.html

注意:KClass代表一个类并提供检查功能。 要获取此类的实例,请使用语法:::class文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18294.html

函数参考

Kotlin函数用于获得使用双冒号的函数参考。 函数引用可以在另一个函数中用作参数。 要在另一个函数中使用此引用,使用::运算符:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18294.html

fun isPositive(x: Int) = x> 0
Kotlin
fun isPositive(x: Int) = x> 0  
val number = listOf(-10,-5,0,5,10)  
print(number.filter(::isPositive))
Kotlin

Kotlin函数引用示例文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18294.html

fun main(args: Array<String>) {
    fun isPositive(x: Int) = x > 0
    val numbers = listOf(-10, -5, 0, 5, 10)
    println(numbers.filter(::isPositive))
}
Kotlin

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18294.html

[5,10]
Shell

在上面的程序中::isPositive是函数类型(Int)-> Boolean的值。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18294.html

重载函数引用运算符(::)

当从上下文中获知预期类型时,运算符::可以与重载函数一起使用。例如:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18294.html

创建一个isPositive()函数,它接受两种不同的IntString类型,并使用不同的类型参数调用此函数。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18294.html

fun main(args: Array<String>) {
    fun isPositive(x: Int) = x > 0
    fun isPositive(s: String) = s== "kotlin" || s == "Kotlin"

    val numbers = listOf(-10,-5,0,5,10)
    val strings = listOf("kotlin", "program")

    println(numbers.filter(::isPositive))
    println(strings.filter(::isPositive))
}
Kotlin

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18294.html

[5, 10]
[kotlin]
Shell

属性参考

也可以在Kotlin中访问属性作为第一类对象,以访问可以使用的对象属性::运算符:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18294.html

要评估KProperty <Int>类型的属性对象,使用表达式::variableName。表达式:: variableName可使用get()函数使用namereadits值来检索属性名称。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18294.html

要重置可变类型属性的值,引用属性具有set()方法。示例代码 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18294.html

fun main(args: Array<String>) {
    println(::x.get())
    println(::x.name)
    println(::y.set(10))
}
val x = 5
var y = 5
Kotlin

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18294.html

5
x
kotlin.Unit
Shell

访问成员类的属性

属性引用还可以访问其他类成员的属性,例如:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18294.html

class A(val x: Int)
fun main(args: Array<String>) {
    val prop = A::x
    println(prop.get(A(5)))
}
Kotlin

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18294.html

5

//原文出自【易百教程】,商业转载请联系作者获得授权,非商业转载请保留原文链接:https://www.yiibai.com/kotlin/kotlin-reflection.html#article-start文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18294.html

文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/18294.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ymba/18294.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定