参考链接
示例来自bilibili Kotlin语言深入解析 张龙老师的视频
12 通过KClass获取泛型类型参数
class MyTestClass<K,V> {
val k:K ? = null
val v:V ? = null
}
fun main() {
val myTestClassType = MyTestClass::class
println(myTestClassType.typeParameters)
println(myTestClassType.typeParameters.size)
println("first property type "+myTestClassType.typeParameters[0])
println("second property type "+myTestClassType.typeParameters[1])
}
class HelloKotlin12 {
}
13 通过KClass获取一个类的父类信息
class MySerializable:Serializable,MyInterface
interface MyInterface
fun main() {
val mySerializable = MySerializable::class
println(mySerializable.superclasses)
}
class HelloKotlin13 {
}
14 通过KClass获取一个类的属性信息
class MyTestClass2(var a:String,val flag:Boolean,var age:Int)
fun main() {
val myTestClass2 = MyTestClass2::class
println(myTestClass2.memberProperties)
}
class HelloKotlin14 {
}
15 通过KClass获取一个类的普通方法
class MyTestClass3{
fun printSth(){
println("printSth")
}
fun getSth():String{
return "getSth"
}
}
fun main() {
val myTestClass3 = MyTestClass3::class
println(myTestClass3.memberFunctions)
}
class HelloKotlin15 {
}
16 通过KClass获取一个类的构造方法
class MyTestClass4(value: Int) {
constructor(value: Int, color: String) : this(value) {
println("secondary constructor with string")
}
constructor(value: Int, full: Boolean) : this(value) {
println("secondary constructor with boolean")
}
}
fun main() {
val myTestClass4 = MyTestClass4::class
val constructors = myTestClass4.constructors
println(constructors)
}
class HelloKotlin16 {
}
17 通过KClass获取一个类的普通方法并调用
class MyTestClass5 {
fun printSomething(name: String) {
println("printSomething $name")
}
fun printNothing() {
println("printNothing")
}
}
fun main() {
val myTestClass5 = MyTestClass5::class
val myTestClass5Instance = MyTestClass5()
val functionToInvoke1 = myTestClass5.functions.find { it.name == "printSomething" }
functionToInvoke1?.call(myTestClass5Instance, "AAAA")
val functionToInvoke2 = myTestClass5.functions.find { it.name == "printNothing" }
functionToInvoke2?.call(myTestClass5Instance)
}
class HelloKotlin17 {
}
18 通过KClass获取一个类的属性并调用其get方法
class MyTestClass6() {
var name: String = "hello"
}
fun main() {
val myTestClass6Class = MyTestClass6::class
val myTestClass6Instance = MyTestClass6()
val propertyToInvoke = myTestClass6Class.memberProperties.find { it.name == "name" }
println(propertyToInvoke?.call(myTestClass6Instance))
println(propertyToInvoke?.get(myTestClass6Instance))
println(propertyToInvoke?.getter?.call(myTestClass6Instance))
}
class HelloKotlin18 {
}
19 通过KClass获取一个类的属性并get set它的值
class MyTestClass7{
var name: String = "hello"
var authorName: String = "Tom"
}
fun main() {
val myTestClass7Class = MyTestClass7::class
val myTestClass7Instance = MyTestClass7()
val propertyToInvoke = myTestClass7Class.memberProperties.find { it.name == "name" }
println(propertyToInvoke?.call(myTestClass7Instance))
println(propertyToInvoke?.get(myTestClass7Instance))
println(propertyToInvoke?.getter?.call(myTestClass7Instance))
println(myTestClass7Instance.name)
if (propertyToInvoke is KMutableProperty<*>){
propertyToInvoke.setter.call(myTestClass7Instance,"hi")
}
println(propertyToInvoke?.call(myTestClass7Instance))
println(propertyToInvoke?.get(myTestClass7Instance))
println(propertyToInvoke?.getter?.call(myTestClass7Instance))
println(myTestClass7Instance.name)
}
class HelloKotlin19 {
}
20 通过KClass获取一个类的伴生对象 并反射调用伴生的方法
class MyTestClass8{
companion object{
fun method(){
println("method")
}
}
}
fun main() {
val myTestClass8Class = MyTestClass8::class
val companionObj = myTestClass8Class.companionObject
println(companionObj)
val methodToInvoke = companionObj?.functions?.find { it.name == "method" }
methodToInvoke?.call(MyTestClass8)
MyTestClass8.method()
}
class HelloKotlin20 {
}
21 通过KClass反射创建实例
class MyTestClass9(val value: Int = 0) {
fun printSomething(name: String) {
println("printSomething $name")
}
fun printNothing() {
println("printNothing $value")
}
}
fun main() {
val myTestClass9KClass = MyTestClass9::class
val myTestClass9Instance = myTestClass9KClass.createInstance()
myTestClass9KClass.functions.find { it.name == "printSomething" }
?.call(myTestClass9Instance, "AAA")
myTestClass9KClass.functions.find { it.name == "printNothing" }?.call(myTestClass9Instance)
}
class HelloKotlin21 {
}
22 通过KClass反射调用有参构造方法
class MyTestClass10(var name: String) {
var price = 0.0
constructor() : this("默认商品") {
price = 0.0
}
constructor(name: String, price: Double) : this(name) {
this.price = price
}
}
fun main() {
val myTestClass10KClass = MyTestClass10::class
val myTestClass10Instance = myTestClass10KClass.createInstance()
println(myTestClass10Instance.name)
println(myTestClass10Instance.price)
println(myTestClass10KClass.memberProperties.find { it.name == "name" }
?.call(myTestClass10Instance))
println(myTestClass10KClass.memberProperties.find { it.name == "price" }
?.call(myTestClass10Instance))
val cons = myTestClass10KClass.constructors
cons.forEach {
if (it.parameters.size == 2) {
val instance2 = it.call("python", 34)
println(myTestClass10KClass.memberProperties.find { it.name == "name" }
?.call(instance2))
println(myTestClass10KClass.memberProperties.find { it.name == "price" }
?.call(instance2))
}
}
}
class HelloKotlin22 {
}
23 通过KClass获取一个类的普通方法并调用
class MyTestClass11 {
fun printSomething(name: String) {
println("printSomething $name")
}
fun printNothing() {
println("printNothing")
}
}
fun main() {
val myTestKClass11 = MyTestClass11::class
val myTestKClass5Instance = myTestKClass11.createInstance()
val functions = myTestKClass11.functions
for (function in functions) {
if (function.parameters.size == 2) {
function.call(myTestKClass5Instance, "xxxx")
}
if (function.parameters.size == 1) {
function.call(myTestKClass5Instance)
}
}
}
class HelloKotlin23 {
}
24 通过KClass获取一个类的属性并get set它的值
class MyTestClass12 {
var name: String = "Flutter"
val price: Double = 34.5
}
fun main() {
val myTestClass12KClass = MyTestClass12::class
val myTestClass12Instance = myTestClass12KClass.createInstance()
val properties = myTestClass12KClass.memberProperties
properties.forEach {
when (it.name) {
"name" -> {
val kMutableProperty = it as KMutableProperty1<MyTestClass12, String>
kMutableProperty.set(myTestClass12Instance, "Kotlin")
println(it.get(myTestClass12Instance))
}
"price" -> {
println(it.get(myTestClass12Instance))
}
}
}
}
class HelloKotlin24 {
}
25 顶层变量的反射 vs 普通变量的反射调用
class MyTestClass13 {
var name: String = "Flutter"
}
var test = "test"
fun main() {
val topProp = ::test
topProp.set("changed value")
println(topProp.get())
println(test)
println("==========通过实例反射调用属性========")
val myTestClass13Instance = MyTestClass13::class.createInstance()
val name = myTestClass13Instance::name
name.set("ruby")
println(name.get())
println("==========通过KClass反射调用属性========")
val name2 = MyTestClass13::name
name2.set(myTestClass13Instance,"Kotlin")
println(name2.get(myTestClass13Instance))
}
class HelloKotlin25 {
}
26 查找一个用作Kotlin属性的getter的幕后字段或Java方法
class MyTestClass14 {
var name: String = "java"
val price: Double = 34.5
}
var myTest = "myTest"
fun main() {
val topProp = ::myTest
println(topProp.javaField)
println(topProp.javaSetter)
println(topProp.javaGetter)
val n = MyTestClass14::name
println(n.javaField)
println(n.javaSetter)
println(n.javaGetter)
val p = MyTestClass14::price
println(p.javaField)
println(p.javaGetter)
}
class HelloKotlin26 {
}
|