如何将 kotlin class 或 kotlin class 实例作为参数传递

How to pass kotlin class or kotlin class instance as arguments

见以下代码:

class Ex() {
    fun m(i: Object) {
    }
}

class Ex2() {
    fun m2() {
        Ex().m(this)
    }    
}

如何让Ex#m接收Ex2实例,我现在可以传this.javaClass作为参数,但是太麻烦了,什么是"this"的Class

更新

fun m(i: Object) 更改为 fun m(i: Ex2) 可以修复当前示例,但我希望 KotlinObjectClass 使 Ex#m 接收变量类型参数

更新 使用 Any 可以解决 "variable type" 问题,但我希望更确切地说,我希望只接收类型 KotlinClassObjectInstance

这是您要找的吗?

interface MyKotlin
class Ex {
  fun m(i: MyKotlin) {
  }
}
class Ex2 : MyKotlin {
  fun m2() {
    Ex().m(this)
  }
}
class Ex3 : MyKotlin {
  fun m3() {
    Ex().m(this)
  }
}

您可以像这样使用 reified type parameters

class Ex() {
    inline fun <reified T> m() {
        println(T::class)
    }
}

class Ex2() {
    fun m2() {
        Ex().m<Ex2>()
    }
}

现在 Ex2().m2() 将打印 class Ex2